我需要在我的项目中使用Fabric JS创建用于绘制多边形的工具。我找到了一个脚本https://github.com/taqimustafa/fabricjs-polygon它是完美的我。但此脚本与织物1一起工作。6.3而我使用1.7.11.
而且我对织物1有一个小问题。7.11和activeShape。ActiveShape仅在第一个点和最后一个点之间的90度区域中绘制。
以下是示例:https://codepen.io/daer_ru/pen/pdOMyX
var min = 99;
var max = 999999;
var polygonMode = true;
var pointArray = new Array();
var lineArray = new Array();
var activeLine;
var activeShape = false;
var canvas
$(window).load(function(){
prototypefabric.initCanvas();
$('#create-polygon').click(function() {
prototypefabric.polygon.drawPolygon();
});
});
var prototypefabric = new function () {
this.initCanvas = function () {
canvas = window._canvas = new fabric.Canvas('c');
canvas.setWidth($(window).width());
canvas.setHeight($(window).height()-$('#nav-bar').height());
//canvas.selection = false;
canvas.on('mouse:down', function (options) {
if(options.target && options.target.id == pointArray[0].id){
prototypefabric.polygon.generatePolygon(pointArray);
}
if(polygonMode){
prototypefabric.polygon.addPoint(options);
}
});
canvas.on('mouse:up', function (options) {
});
canvas.on('mouse:move', function (options) {
if(activeLine && activeLine.class == "line"){
var pointer = canvas.getPointer(options.e);
activeLine.set({ x2: pointer.x, y2: pointer.y });
var points = activeShape.get("points");
points[pointArray.length] = {
x:pointer.x,
y:pointer.y
}
activeShape.set({
points: points
});
canvas.renderAll();
}
canvas.renderAll();
});
};
};
prototypefabric.polygon = {
drawPolygon : function() {
polygonMode = true;
pointArray = new Array();
lineArray = new Array();
activeLine;
},
addPoint : function(options) {
var random = Math.floor(Math.random() * (max - min + 1)) + min;
var id = new Date().getTime() + random;
var circle = new fabric.Circle({
radius: 5,
fill: '#ffffff',
stroke: '#333333',
strokeWidth: 0.5,
left: (options.e.layerX/canvas.getZoom()),
top: (options.e.layerY/canvas.getZoom()),
selectable: false,
hasBorders: false,
hasControls: false,
originX:'center',
originY:'center',
id:id
});
if(pointArray.length == 0){
circle.set({
fill:'red'
})
}
var points = [(options.e.layerX/canvas.getZoom()),(options.e.layerY/canvas.getZoom()),(options.e.layerX/canvas.getZoom()),(options.e.layerY/canvas.getZoom())];
line = new fabric.Line(points, {
strokeWidth: 2,
fill: '#999999',
stroke: '#999999',
class:'line',
originX:'center',
originY:'center',
selectable: false,
hasBorders: false,
hasControls: false,
evented: false
});
if(activeShape){
var pos = canvas.getPointer(options.e);
var points = activeShape.get("points");
points.push({
x: pos.x,
y: pos.y
});
var polygon = new fabric.Polygon(points,{
stroke:'#333333',
strokeWidth:1,
fill: '#cccccc',
opacity: 0.3,
selectable: false,
hasBorders: false,
hasControls: false,
evented: false
});
canvas.remove(activeShape);
canvas.add(polygon);
activeShape = polygon;
canvas.renderAll();
}
else{
var polyPoint = [{x:(options.e.layerX/canvas.getZoom()),y:(options.e.layerY/canvas.getZoom())}];
var polygon = new fabric.Polygon(polyPoint,{
stroke:'#333333',
strokeWidth:1,
fill: '#cccccc',
opacity: 0.3,
selectable: false,
hasBorders: false,
hasControls: false,
evented: false
});
activeShape = polygon;
canvas.add(polygon);
}
activeLine = line;
pointArray.push(circle);
lineArray.push(line);
canvas.add(line);
canvas.add(circle);
canvas.selection = false;
},
generatePolygon : function(pointArray){
var points = new Array();
$.each(pointArray,function(index,point){
points.push({
x:point.left,
y:point.top
});
canvas.remove(point);
});
$.each(lineArray,function(index,line){
canvas.remove(line);
});
canvas.remove(activeShape).remove(activeLine);
var polygon = new fabric.Polygon(points,{
stroke:'#333333',
strokeWidth:0.5,
fill: 'red',
opacity: 1,
hasBorders: false,
hasControls: false
});
canvas.add(polygon);
activeLine = null;
activeShape = null;
polygonMode = false;
canvas.selection = true;
}
};
2条答案
按热度按时间qc6wkl3g1#
创建多边形时需要使用
objectCaching:false
。在制作完完整的多边形后,不要使用它,所以它会创建自己的对象缓存,这样如果没有修改,它就不会在下一次渲染调用时渲染。这里更新了codepen
imzjd6km2#
作为提问者,我有确切的需要。补充一些信息:
如果您也在这样做,您可能想知道多边形被移动、调整大小、旋转等时多边形点的坐标。
但一旦多边形被创建,
points
将不会再次更改,因此您无法直接获得正确的坐标。在织物2之前。0,多边形点相对于其中心,您希望通过以下操作计算坐标:How to get polygon points in Fabric.js
在2.0之后,多边形点对于画布是绝对的,您可以:如何在fabricJS中使用transformMatrix转换点?