Verwenden Sie immer lokale Koordinaten, um Formen zu definieren.
Beim Rendern von Inhalten, die umgewandelt werden sollen, sollte der Inhalt in einem eigenen (lokalen) Koordinatensystem vorliegen. Denk an ein Bild. Der obere linke Pixel ist immer auf 0,0 auf dem Bild, egal wo Sie es rendern. Die Pixel befinden sich an ihren lokalen Koordinaten. Wenn sie gerendert werden, werden sie über die aktuelle Transformation in die (Welt-) Leinwandkoordinaten verschoben.
Also, wenn Sie Ihre Form mit Koordinaten auf seinem lokalen machen, den Drehpunkt an seinem lokalen Ursprung machen (0,0) die Anzeigekoordinaten werden separat als Welt gespeichert Koordinaten
var shape = {
top: -30, // local coordinates with rotation origin
left: -60, // at 0,0
width: 120,
height: 60,
world : {
x : canvas.width/2,
y : canvas.height/2,
rot : Math.PI/12, // 15deg clockwise
}
};
Sie jetzt don‘ Ich muss mich damit beschäftigen, vorwärts und rückwärts zu übersetzen ... blah blah total pain.
Gerade
ctx.save();
ctx.translate(shape.world.x,shape.world.y);
ctx.rotate(shape.world.rot);
ctx.fillRect(shape.left, shape.top, shape.width, shape.height)
ctx.restore();
oder Ereignis schneller und die Notwendigkeit speichern verwenden zu beseitigen und
ctx.setTransform(1,0,0,1,shape.world.x,shape.world.y);
ctx.rotate(shape.world.rot);
ctx.fillRect(shape.left, shape.top, shape.width, shape.height);
Die lokale Form Ursprung (0,0) wieder herzustellen ist, wo die Transformation Übersetzung platziert.
vereinfacht dies erheblich viel von der Arbeit, die
getan werden muss, um
var canvas = document.getElementById('canvas');
canvas.width = 300;
canvas.height= 150;
var ctx = canvas.getContext('2d');
ctx.fillStyle = "black";
ctx.strokeStyle = "red";
ctx.lineWidth = 2;
var shape = {
top: -30, // local coordinates with rotation origin
left: -60, // at 0,0
width: 120,
height: 60,
world : {
x : canvas.width/2,
y : canvas.height/2,
rot : Math.PI/12, // 15deg clockwise
}
};
function draw() {
ctx.setTransform(1,0,0,1,0,0); // to clear use default transform
ctx.clearRect(0, 0, canvas.width, canvas.height);
// you were scaling the shape, that can be done via a transform
// once you have moved the shape to the world coordinates.
ctx.setTransform(1,0,0,1,shape.world.x,shape.world.y);
ctx.rotate(shape.world.rot);
// after the transformations have moved the local to the world
// you can ignore the canvas coordinates and work within the objects
// local. In this case showing the unscaled box
ctx.strokeRect(shape.left, shape.top, shape.width, shape.height);
// and a line above the box
ctx.strokeRect(shape.left, shape.top - 5, shape.width, 1);
ctx.scale(0.5,0.5); // the scaling you were doing
ctx.fillRect(shape.left, shape.top, shape.width, shape.height);
}
canvas.addEventListener('click', function() {
shape.width += 15;
shape.left -= 15/2;
shape.world.rot += Math.PI/45; // rotate to illustrate location
// of local origin
var distToMove = 15/2;
shape.world.x += Math.cos(shape.world.rot) * distToMove;
shape.world.y += Math.sin(shape.world.rot) * distToMove;
draw();
});
// no need to use requestAnimationFrame (RAF) if you are not animation
// but its not wrong. Nor do you need to bind this (in this case
// this = window) to the callback RAF does not bind a context
// to the callback
/*window.requestAnimationFrame(draw.bind(this));*/
requestAnimationFrame(draw); // functionaly identical
// or just
/*draw()*/ //will work
body { font-family : Arial,"Helvetica Neue",Helvetica,sans-serif; font-size : 12px; color : #242729;} /* SO font currently being used */
canvas { border: 1px solid red; }
<canvas id="canvas"></canvas>
<p>Click to grow "and rotate" (I add that to illustrate the local origin)</p>
<p>I have added a red box and a line above the box, showing how using the local coordinates to define a shape makes it a lot easier to then manipulate that shape when rendering "see code comments".</p>
Es wegen 'ctx.translate trieb ist (x, y);' und 'ctx.translate (-x, -y);'. Versuche sie zu kommentieren! - https://jsfiddle.net/Pugazh/x5gxo1p7/3/ – Pugazh
Ich brauche die übersetzt, um die Form um sein Zentrum zu drehen. – Jomppe