【发布时间】:2016-05-02 22:12:23
【问题描述】:
我有一段代码记录鼠标点击并在最后绘制一个多边形。 绘制多边形后,它会将坐标数组串起来以备将来处理。
我面临的问题是,记录和绘制的和字符串化的是完全不同的两组数据。
例如
(index):169 Mouse click at 543 179
(index):171 Object {x: 543, y: 179} contains value, match as per mouse click
(index):169 Mouse click at 310 592
(index):171 Object {x: 310, y: 592} contains value, match as per mouse click
(index):169 Mouse click at 735 480
(index):171 Object {x: 735, y: 480} contains value, match as per mouse click
(index):246 [{"x":20.5,"y":-206.5},{"x":-212.5,"y":206.5},{"x":212.5,"y":94.5}] is stringified value
JS
var coords = [];
var canvas = new fabric.Canvas('canvas');
canvas.on('mouse:down', function (options) {
getCoordinates(options);
});
$('.drawReady').click(function(){
drawMyPolygon(coords, 1);
stringifyTheData();
});
function drawMyPolygon(retData, tmpId) {
var polygon = new fabric.Polygon(retData, {
fill: 'purple',
selectable: true,
lockMovementX: true,
lockMovementY: true,
id: tmpId
});
canvas.add(polygon);
};
function getCoordinates(options) {
var pointer = canvas.getPointer(event.e);
coords.push({x: pointer.x, y: pointer.y});
}
function stringifyTheData() {
var retVal = JSON.stringify(coords);
}
【问题讨论】:
标签: javascript fabricjs