简体   繁体   English

将元素设置为在JCanvas中单击时可见

[英]Set an element to visible on click in JCanvas

I have drawn an mage on the canvas using drawImage() and have set its visibility to false and i want it to be visible on click and mouseover. 我已经使用drawImage()在画布上绘制了一个法师,并将其可见性设置为false,我希望它在单击和鼠标悬停时可见。 How do i do ? 我该怎么办 ? Thanks 谢谢

Here's what i've written so far, but this doesn't work of course 这是我到目前为止所写的,但这当然行不通

$('#scene').drawImage({

            source:'files/gp/js/bigview/avg/aileav.png',
            name:'aileavg',
            x:198,
            y:76,
            width:110,
            height:106,
            fromCenter: false,
            layer: true,
            visible:false,              
            click:function(layer){
                visible:true
                }

    })  

Canvas does not work that way. 画布不能那样工作。 A canvas is not an object container. 画布不是对象容器。 It works like... well... a canvas. 它的工作原理类似于...嗯...一块画布。 When you draw an image to a canvas, it stops being an image and starts being a collection of pixels. 当您将图像绘制到画布上时,它不再是图像,而是开始是像素集合。

When you want something to disappear from a canvas, you either have to erase it by overpainting it with something else or by erasing the whole canvas and drawing everything again except for that object. 当您希望某东西从画布上消失时,您要么通过用其他东西对其进行过度上漆来擦除它,要么擦除整个画布并再次绘制除该对象之外的所有内容。

In jCanvas, disabling layer visibility using the visible property will prevent that layer from being drawn (thus preventing it from responding to mouse events). 在jCanvas中,使用visible属性禁用层visible性将阻止绘制该层(从而阻止其响应鼠标事件)。

If you want the layer to be rendered invisible but still respond to events, the opacity property is the ideal solution. 如果要使该图层不可见但仍对事件做出响应,则opacity属性是理想的解决方案。 You should use the setLayer() method to update the value of the opacity property (setting it to 0 will render the image invisible). 您应该使用setLayer()方法更新opacity属性的值(将其设置为0将使图像不可见)。 Note that you will need to call the drawLayers() method afterwards to render this change on the canvas. 请注意,您将需要drawLayers()调用drawLayers()方法以在画布上呈现此更改。

$('#scene').drawImage({

    source: 'files/gp/js/bigview/avg/aileav.png',
    name: 'aileavg',
    x: 198,
    y: 76,
    width: 110,
    height: 106,
    fromCenter: false,
    layer: true,
    opacity: 0,          
    click: function (layer) {
        $(this).setLayer(layer, {
            opacity: 1
        })
        .drawLayers();
    }

});

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

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