Search code examples
javascriptmouseeventkineticjs

KineticJS, Veeery slow click event on image


I want to click some KineticJS images that is marked with pencircle on the picture:

picture of webpage

The picture with blue circle is working okey.

The picture with yellow circle is working every 50 clicks.

The pictures with red circle is never working.

This is how I add the eventlisteners:

Blue:

    var imgLogo = new Image();
    imgLogo.src = 'http://folk.ntnu.no/rubenra/web/Resources/bobsByggLogo.png';
    imgLogo.onload = function(){
        imageLogo = new Kinetic.Image({
            image: imgLogo,
            x: 10,
            y: 580
        });
        layer.add(imageLogo);
        imageLogo.on('mousedown', function() {
            console.log('logo trykt');
        });
    }

Yellow:

    var imgNextDayButton = new Image();
    imgNextDayButton.src = 'http://folk.ntnu.no/rubenra/web/Resources/nextDayButton.png';
    imgNextDayButton.onload = function(){
        imageNextDayButton = new Kinetic.Image({
            image: imgNextDayButton,
            x: buttonsBound.getAttr('x')+60,
            y: buttonsBound.getAttr('y')+20
        });

        imageNextDayButton.on('mousedown', function() {
            alert('Neste Dag');
            console.log('knapp trykt');
        });

        layer.add(imageNextDayButton);


    }

Red:

    var imgWarn = new Image();
    imgWarn.src = 'http://folk.ntnu.no/rubenra/web/Resources/shapeWarn.png';
    imgWarn.onload = function(){
        imageWarn = new Kinetic.Image({
            image: imgWarn,
            x: xstart+100,
            y: ystart+45,
            width: imgWarn.width,
            heigth: imgWarn.height
        });
        imageWarn.on('mousedown',function(){
            alert('Show Warning to ' + name);
        })
        group.add(imageWarn);
    }

And here is link to the sourcecode. It is not working on jsbin, but it should work in every code editor.

Link to the page on server: Page on server


Solution

  • @Yechoua Guedj was right that it was because of the way I resized my canvas. A simple way that solved all my eventproblems is like this.

    window.onresize = function () {
            var width = $("#fullscreenDiv").innerWidth();
            var height = width * 9 / 16;
            xScale = (width/initialWidth)*initialScale.x;
            var newScale = {x: xScale, y: xScale};
            stage.setAttr('width', width);
            stage.setAttr('height', height);
            stage.setAttr('scale', newScale);
            stage.draw();
        }