简体   繁体   English

无法在画布上添加新对象(fabric.js)

[英]can't add new objects on canvas (fabric.js)

I am working on a project using fabric.js and facing this problem. 我正在使用fabric.js进行项目,并且遇到了这个问题。

i have a function fun() and its called on event onclick, so basically i am expecting a circle everytime i click on my screen but as result all i get is a blank screen. 我有一个fun()函数,并且它在onclick事件上被调用,因此基本上我每次在屏幕上单击时都希望有一个圆圈,但结果我得到的只是一个空白屏幕。 alerting the objetcs sure gives me the number of objects with the type circle and is incremented everytime i click on the screen but i cant see the circles on the screen, its like the cirlces are there but not visible. 提醒objetcs确保为我提供了类型为circle的对象数量,并且每次我在屏幕上单击时都会增加,但是我看不到屏幕上的圆圈,就像圆圈在那儿但不可见。

here is the code: 这是代码:

<html>
<head>
<script type="text/javascript" src="C:\Users\Sunny\Documents\6th Sem Ka Maal\Graph\project\GUI\fabric.js-1.5.0\dist\fabric.js"></script>
<script>

var canvas = new fabric.Canvas('p');
function fun(event){

var x = event.clientX;
var y = event.clientY;
alert(x);
alert(y);
alert(canvas);
var circle= new fabric.Circle({
radius: 50, fill: 'green', left: x, top: y
});
alert(circle);
canvas.add(circle);

}
</script>
</head>
<body>
<canvas id="p" height="993" width="1920" onclick="fun(event)"></canvas>
</body>
</html>

why am i not able to see the circles? 为什么我看不到圈子?

Try wiring up the event in Javascript instead, using Fabric's on() listener. 尝试使用Fabric的on()侦听器代替Java事件。

canvas.on('mouse:up', fun);

Read more about fabric events here: 在此处阅读有关结构事件的更多信息:

https://github.com/kangax/fabric.js/wiki/Working-with-events https://github.com/kangax/fabric.js/wiki/Working-with-events

Last year I was messing around with Fabric for an OU project for point-to-point drawing, you can have a look at it here might give you some ideas. 去年,我在Fabric中进行了点对点绘图的OU项目,您可以在这里查看一下,这可能会给您一些想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM