我试图在画布上显示每个对象的某些属性,但我很难编写代码来这样做。EG 我想要下面的代码来识别形状、文本和图像对象之间的区别,并根据类型显示属性。
console.log 的属性:
对于画布上的每个对象,检查是否 -
我需要添加/删除/更改什么才能使其工作?
这是一个 JS 小提琴示例:https : //jsbin.com/vevihikefo/1/edit? html,js,console,output
canvas.getObjects().forEach(object=>{
if(object.isType('xyz')) { // object is a shape
console.log(object.scaleX, object.fill);
} else if(object.isType('text')) { // object is a text
console.log(object.text, object.fontFamily, object.fontSize*3, object.scaleX, object.fill);
} else { // object is an image
console.log(object.name, object.scaleX, object.fill);
}
})
有两件事需要添加/更改,以使其工作......
object.isType('xyz')
为!object.isType('text') && !object.isType('image')
(因为没有任何类型称为 'xyz' )fabric.Object.prototype.objectCaching = false;
var canvas = new fabric.Canvas('canvas');
canvas.add(new fabric.Text('foo', {
fontFamily: 'Roboto',
left: 100,
top: 100,
fontSize: 25
}));
canvas.add(new fabric.Text('help', {
fontFamily: 'Roboto',
left: 100,
top: 200,
fontSize: 25
}));
canvas.add(new fabric.Triangle({
fill: 'green',
left: 200,
top: 200
}));
fabric.Image.fromURL('http://fabricjs.com/assets/pug_small.jpg', function(myImg) {
//i create an extra var for to change some image properties
var img1 = myImg.set({
left: 200,
top: 0,
width: 150,
height: 150
});
canvas.add(img1);
getAttributes();
});
canvas.renderAll();
function getAttributes() {
canvas.getObjects().forEach(object => {
if (!object.isType('text') && !object.isType('image')) { // object is a shape
console.log(object.scaleX, object.fill);
} else if (object.isType('text')) { // object is a text
console.log(object.text, object.fontFamily, object.fontSize * 3, object.scaleX, object.fill);
} else if (object.isType('image')) { // object is an image
console.log(object.name, object.scaleX, object.fill);
}
});
}
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>
<canvas id="canvas" width="800" height="600" style="border: 1px solid #d3d3d3; position: absolute;">
Your browser does not support the canvas element.
</canvas>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句