使用Fabric.js为画布启用和禁用鼠标事件 [英] Enable and disable mouse events for canvas using Fabric.js

查看:366
本文介绍了使用Fabric.js为画布启用和禁用鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Fabric.js在画布上绘制矩形。绘制矩形后,我想禁用该对象上的所有事件。我尝试使用 canvas .__ eventListeners [mouse:down] = []; 但是在清除了对象选择后,画布无法应用任何事件。

I am using Fabric.js to draw rectangles on a canvas. After drawing a rectangle, I want to disable all events on that object. I have tried to do it using canvas.__eventListeners["mouse:down"] = []; but then after the object selection is cleared, the canvas can't apply any events.

<html lang="en" >

<head>

    <meta charset="utf-8" />

    <title>HTML5 canvas - Image color picker | Script Tutorials</title>

    <link href="index.css" rel="stylesheet" type="text/css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.1.0/fabric.all.min.js" ></script>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 



</head>

<body>
    <div class="container">
    <div class="column1">
        <canvas id="panel" width="700" height="350"></canvas>   
    </div>
    <div style="clear:both;"></div>
    <div id="log"></div>
</div>
</body>
<script>
    (function() {
        var canvas = new fabric.Canvas('panel');
        var clicks=0;
        var x1=0;var y1=0;
        var rect;
        canvas.on('mouse:down', function(e){
         //check if you clicked on an object that exists canvas
            if(e.target == undefined){
                    if (clicks == 0) {
                        var pointer=canvas.getPointer(event.e);
                        console.log(pointer);
                        x1 = pointer.x;
                        y1 =pointer.y;
                        console.log("Start Pointer " +x1 ,y1);
                        clicks++;
                    } 
                    else 
                    {
                        var endpointer=canvas.getPointer();
                        console.log(endpointer);
                        var endx=endpointer.x;
                        var endy=endpointer.y;
                        console.log("Endpointer  " +endx ,endy);
                        console.log("x and y"+x1,y1);
                        var newwidth=endpointer.x- x1;
                        var newheight=endpointer.y - y1;

                        rect=new fabric.Rect({
                                left:x1,
                                top: y1,
                                originX :'left',
                                originY :'top',
                                width:newwidth,
                                height:newheight,
                                selectable: true,
                                evented:false,
                                fill:'red',
                                opacity :0.3

                            });
                        canvas.add(rect);
                        //console.log(rect.setWidth(pointer2.x- x1 ));
                        //console.log(rect.setHeight( pointer2.y - y1));
                        canvas.renderAll();
                        clicks=0;   

                    }   
                }
                else
                {

                    //canvas.getActiveObject().remove();
                    canvas.__eventListeners["mouse:down"] = [];
                }
            });

            canvas.on('object:moving',function(){
                var bound=rect.getBoundingRect();
                console.log(bound.left,bound.top,bound.width,bound.height);
            });
            canvas.on('object:scaling',function(){
                var bound=rect.getBoundingRect();
                console.log(bound.left,bound.top,bound.width,bound.height);
            });
            canvas.on('object:selected',function(e){

                document.onkeydown = function(e) {
                    if (e.keyCode === 27||e.button==3) {
                      e.preventDefault();
                        canvas.getActiveObject().remove();
                    }

                }

            });

            fabric.Image.fromURL('fedex.jpg', function (img) {
                canvas.add(img.set({
                    width: 700,
                    height:350,
                    left: 350,
                    top: 175,
                    selectable: false,
                }));
            });
    })();
</script>

推荐答案

使用方法 canvas.off('mouse:down',eventHandler)使用 canvas.on注册的事件处理程序('mouse:down ',eventHandler)可以删除。确保将相同的函数传递给两个调用(例如,通过将处理程序分配给变量)。

With the method canvas.off('mouse:down', eventHandler) event handlers registered using canvas.on('mouse:down', eventHandler) can be removed. Be sure to pass the same function into both calls (e.g. by assigning the handler to a variable).

此方法适用于结构中的所有 Observable (请参阅API doc: http://fabricjs.com/docs/fabric.Observable.html#off

This method is available on all Observables in fabric (see API doc: http://fabricjs.com/docs/fabric.Observable.html#off)

这篇关于使用Fabric.js为画布启用和禁用鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆