简体   繁体   English

ExtJS绘制包和鼠标事件

[英]ExtJS draw package and mouse events

I want to be able to draw some geometry on canvas, using ExtJS draw package . 我希望能够使用ExtJS draw package在画布上绘制一些几何图形。 But I also want to provide some interactivity, such as moving of a drawn circle or highlighting of geometry when a user clicks on it. 但是我还想提供一些交互性,例如,移动绘制的圆或在用户单击时突出显示几何。 But I could not achieve this. 但是我无法做到这一点。 For example, this is what I've tried: 例如,这是我尝试过的:

Ext.create({
    xtype: 'draw',
    renderTo: document.body,
    width: 600,
    height: 400,
    sprites: [{
       type: 'circle',
       cx: 100,
       cy: 100,
       r: 50,
       fillStyle: '#1F6D91',
       listeners: {
           click: function () {
               alert("click!");
           }
       }
   }]
});

When I click on the circle, nothing happens. 当我单击圆圈时,什么也没发生。 So, my question is whether it is possible and if yes, how can we do this? 因此,我的问题是是否可能,如果可以,我们如何做到这一点?

Two things that you will need to make this work: 要完成这项工作,您需要完成两件事:

1 - The listeners must be added to the draw container and not to the sprite itself. 1-侦听器必须添加到绘图容器中,而不是精灵本身。 (The events will be spriteclick , spritedblclick , spritetap , spritemousedown , spritemouseup and so on.. check a list here: http://docs.sencha.com/extjs/6.2.0/classic/Ext.draw.Container.html#event-spriteclick ). (该事件将被spriteclickspritedblclickspritetapspritemousedownspritemouseup等..检查列表在这里: http://docs.sencha.com/extjs/6.2.0/classic/Ext.draw.Container.html# event-spriteclick )。

2 - You will need to add the Ext.draw.plugin.SpriteEvents to your Draw container so it will be able to listen to SpriteEvents. 2-您需要将Ext.draw.plugin.SpriteEvents添加到Draw容器中,以便它能够侦听SpriteEvents。

Try the code below: 请尝试以下代码:

var drawContainer = Ext.create('Ext.draw.Container', {
    plugins: ['spriteevents'],
    renderTo: Ext.getBody(),
    width: 200,
    height: 200,
    sprites: [{
        type: 'circle',
        fillStyle: '#79BB3F',
        r: 50,
        x: 100,
        y: 100
    }],
    listeners: {
        spriteclick: function (item, event) {
            var sprite = item && item.sprite;
            if (sprite) {
                sprite.setAttributes({fillStyle: 'red'});
                sprite.getSurface().renderFrame();
            }
        }
    }
});

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

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