使用角度.js将画布坐标存储到本地存储
Store canvas coordinates to local storage using angular.js
我想使用角度将绘图画布坐标存储到本地存储.js我得到了坐标,但无法将值推送到本地存储。
从中获取值
draw(lastX, lastY, currentX, currentY);
并将价值存储到本地存储
app.controller('app', function ($scope, $http, $localStorage) {
// Set a default
$scope.$storage = $localStorage.$default({
"lines": []
});
$scope.cloneItem = function () {
$scope.$storage.lines.push({
"lastX":lastX ,
"lastY": lastY,
"currentX": currentX,
"currentY": currentY
});
}
});
我无法获取值lastX lastY currentX currentY
普伦克演示
Plunker 对我不起作用,当我单击按钮时,lastX 变量未定义。但是我复制粘贴的代码并手动输入值,似乎它可以工作,所以我认为问题是您应该在存储它们之前检查变量是否已定义。
此外,在设置其默认值之前,您需要在 localStorage 中输入 init 行,否则稍后会抛出错误,尝试存储在未定义的值中。
http://embed.plnkr.co/kbrRd2fYlL3oW07iI8Hm/
在
作用域设置变量
app.controller('app', function($scope, $http, $localStorage) {
// Set a default
$scope.$storage = $localStorage.$default({
"lines": []
});
$scope.cloneItem = function() {
$scope.$storage.lines.push({
"lastX": $scope.lastX,
"lastY": $scope.lastY,
"currentX": $scope.currentX,
"currentY": $scope.currentY
});
alert('$scope.lastX11111 -' + $scope.lastX)
}
});
自
app.directive("drawing", function() {
return {
restrict: "A",
link: function(scope, element) {
var ctx = element[0].getContext('2d');
// variable that decides if something should be drawn on mousemove
var drawing = false;
element.bind('mousedown', function(event) {
if (event.offsetX !== undefined) {
scope.lastX = event.offsetX;
scope.lastY = event.offsetY;
} else {
scope.lastX = event.layerX - event.currentTarget.offsetLeft;
scope.lastY = event.layerY - event.currentTarget.offsetTop;
}
// begins new line
ctx.beginPath();
drawing = true;
});
element.bind('mousemove', function(event) {
if (drawing) {
// get current mouse position
if (event.offsetX !== undefined) {
scope.currentX = event.offsetX;
scope.currentY = event.offsetY;
} else {
scope.currentX = event.layerX - event.currentTarget.offsetLeft;
scope.currentY = event.layerY - event.currentTarget.offsetTop;
}
draw(scope.lastX, scope.lastY, scope.currentX, scope.currentY);
// console.log(lastX, lastY, currentX, currentY);
// set current coordinates to last one
scope.lastX = scope.currentX;
// console.log(lastX, lastY, currentX, currentY);
scope.lastY = scope.currentY;
}
});
element.bind('mouseup', function(event) {
// stop drawing
drawing = false;
});
// canvas reset
function reset() {
element[0].width = element[0].width;
}
function draw(lX, lY, cX, cY) {
// line from
ctx.moveTo(lX, lY);
// to
ctx.lineTo(cX, cY);
// color
ctx.strokeStyle = "#4bf";
// draw it
ctx.stroke();
}
}
};
});
http://plnkr.co/edit/Qcqua0gjoAfya6xkQFiX?p=preview
相关文章:
- EaseJS拖放;放下(动画CC)电影剪辑的鼠标坐标
- 如何在d3.js中返回路径的y坐标
- 将作用域存储在JSON中
- 使用javascript存储变量的最安全方式
- 当图像转换为本地存储的DataURL时,EXIF被删除
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- AngularJS-如何在mousemove上存储鼠标坐标
- Javascript 地理位置:将坐标存储在数组中
- 使用谷歌地图和Angular JS.正在尝试将坐标存储在变量中
- 将谷歌地图 api latLng 坐标存储到 js 数组
- 使用角度.js将画布坐标存储到本地存储
- 如何将坐标存储到不同的变量中?
- 如何绘制一个矩形,其坐标存储在mysql数据库中
- 在数组JAVASCRIPT中存储坐标和各种游戏数据
- 如何显示动画多个标记从坐标存储在MySQL数据库
- 在javascript中存储数组中的坐标
- 根据提示存储坐标并计算距离
- 在JS中,快速存储XY坐标整数的最高性能方法是什么
- 根据数组中存储的坐标确定在画布上绘制的形状
- 如何使用Javascript将地理位置坐标存储到数组中