Resizing canvas using CSS speeds up mouse action

So, I have this scene that fully explains my problem.

Working violin

Js

        var w = this.w * this.scale;
        var h = this.h * this.scale;
        var x = this.x - w * .5;
        var y = this.y - h * .5;

        //standard Canvas rotation operation
        ctx.save();
        ctx.translate(this.x, this.y);
        ctx.rotate(this.rotation * Math.PI / 180);

        ctx.fillStyle = this.fillStyle;
        ctx.fillRect(w * -0.5, h * -0.5, w, h);
        ctx.restore();

Defective violin

+3
source share
2 answers

You can compare the canvas size with CSS and make the necessary adjustments.

eg.

var cssScale = [canvas.getSize().x / canvas.get('width'),
                canvas.getSize().y / canvas.get('height')];
...
this.setDims(x*cssScale[0], y*cssScale[1], w*cssScale[0], h*cssScale[1]);
...
this.x = (x - this.offset[0]) / cssScale[0] + w * .5;
this.y = (y - this.offset[1]) / cssScale[1] + h * .5;

http://jsfiddle.net/rQkSF/

+1
source

Not sure if I understand the question, but try resetting the size of the canvas height and width attributes to fit the CSS when resizing.

0
source

Source: https://habr.com/ru/post/1774249/


All Articles