gpt4 book ai didi

javascript - 调用 setTransform 后 Canvas 不旋转

转载 作者:行者123 更新时间:2023-11-28 18:31:55 27 4
gpt4 key购买 nike

我有一个 HTML5 Canvas ,我在其中画一条线并围绕中心旋转它。我想在每次绘制时重置 Canvas 的变换,然后重新平移/旋转 Canvas ,但它似乎没有旋转。我有一个工作示例,其中存储了先前的旋转,并且仅按 Angular 差进行旋转(显然,在本示例中我没有重置转换),但在此代码的未经清理的版本中,它没有提供足够的解决方法.

我创建了两个 fiddle ,the first version我在其中重置了转换,但它无法正常运行并且 the second version我在其中存储了 Angular 差异并且它正常工作。下面复制了片段。 如何调整第二个版本使其功能与第一个版本相同?

第一个版本(功能性,非期望的实现)

(function () {    
// dynamic canvas
var dynamic = document.getElementById("dynamic");
var dynamicCtx = dynamic.getContext("2d");

// animation status
var FPS = 10;
var INTERVAL = 1000 / FPS;

dynamicCtx.translate(dynamic.width/2, dynamic.width/2);
var previousRotation_rad = 0;
var rotationCounter_deg = 0;

var myDynamicObject = {
center: dynamic.width / 2,
length: dynamic.width * 0.4,
draw: function () { // example of dynamic animation code
dynamicCtx.clearRect(-this.center, -this.center, 2*this.center, 2*this.center);

// draw the current rotation angle
dynamicCtx.textBaseline = 'middle';
dynamicCtx.textAlign = 'center';
dynamicCtx.fillText(rotationCounter_deg + '°', 0, -this.length);

// draw a line from the center up
dynamicCtx.beginPath();
dynamicCtx.moveTo(0, 0);
dynamicCtx.lineTo(0, -this.length + 20);
dynamicCtx.stroke();

// rotate the canvas
var currentRotation_rad = rotationCounter_deg * Math.PI / 180;
var rotationDifference_rad = currentRotation_rad - previousRotation_rad;
previousRotation_rad = currentRotation_rad;
console.log('rotating', rotationDifference_rad, 'radians');
dynamicCtx.rotate(rotationDifference_rad);
++rotationCounter_deg;
}
};

function drawDynamic() {
myDynamicObject.draw();
// you can add more dynamic objects and draw here
}

function animate() {
setInterval(function () {
// only need to redraw dynamic objects
drawDynamic();
}, INTERVAL);
}

animate(); // entry point for animated (dynamic) objects
})();
#canvasesdiv {
position:relative;
width:400px;
height:400px;
}
#dynamic {
position: absolute;
left: 0;
top: 0;
z-index: 1;
}
<div id="canvasesdiv">
<canvas id="dynamic" width=400 height=400>This text is displayed if your browser does not support HTML5 Canvas</canvas>
</div>

第二个版本(非功能性,所需的实现)

(function () {    
// dynamic canvas
var dynamic = document.getElementById("dynamic");
var dynamicCtx = dynamic.getContext("2d");

// animation status
var FPS = 10;
var INTERVAL = 1000 / FPS;

var rotationCounter_deg = 0;

var myDynamicObject = {
center: dynamic.width / 2,
length: dynamic.width * 0.4,
draw: function () { // example of dynamic animation code
dynamicCtx.setTransform(1, 0, 0, 1, 0, 0);
dynamicCtx.translate(dynamic.width/2, dynamic.width/2);
dynamicCtx.clearRect(-this.center, -this.center, 2*this.center, 2*this.center);

// draw the current rotation angle
dynamicCtx.textBaseline = 'middle';
dynamicCtx.textAlign = 'center';
dynamicCtx.fillText(rotationCounter_deg + '°', 0, -this.length);

// draw a line from the center up
dynamicCtx.beginPath();
dynamicCtx.moveTo(0, 0);
dynamicCtx.lineTo(0, -this.length + 20);
dynamicCtx.stroke();

// rotate the canvas
var currentRotation_rad = rotationCounter_deg * Math.PI / 180;
console.log('rotating', currentRotation_rad, 'radians');
dynamicCtx.rotate(currentRotation_rad);
++rotationCounter_deg;
}
};

function drawDynamic() {
myDynamicObject.draw();
// you can add more dynamic objects and draw here
}

function animate() {
setInterval(function () {
// only need to redraw dynamic objects
drawDynamic();
}, INTERVAL);
}

animate(); // entry point for animated (dynamic) objects
})();
#canvasesdiv {
position:relative;
width:400px;
height:400px;
}
#dynamic {
position: absolute;
left: 0;
top: 0;
z-index: 1;
}
<div id="canvasesdiv">
<canvas id="dynamic" width=400 height=400>This text is displayed if your browser does not support HTML5 Canvas</canvas>
</div>

最佳答案

与 CSS 等不同, Canvas 上下文呈现中间状态,因此必须在绘制任何内容之前应用所有转换。

只需在绘制到 Canvas 和声明之前向上移动旋转变换即可:

(function() {
// dynamic canvas
var dynamic = document.getElementById("dynamic");
var dynamicCtx = dynamic.getContext("2d");

// animation status
var FPS = 10;
var INTERVAL = 1000 / FPS;

var rotationCounter_deg = 0;

var myDynamicObject = {
center: dynamic.width / 2,
length: dynamic.width * 0.4,
draw: function() { // example of dynamic animation code
var currentRotation_rad = rotationCounter_deg * Math.PI / 180;

dynamicCtx.setTransform(1, 0, 0, 1, 0, 0);
dynamicCtx.translate(dynamic.width / 2, dynamic.width / 2);
dynamicCtx.clearRect(-this.center, -this.center, 2 * this.center, 2 * this.center);
dynamicCtx.rotate(currentRotation_rad);

// draw the current rotation angle
dynamicCtx.textBaseline = 'middle';
dynamicCtx.textAlign = 'center';
dynamicCtx.fillText(rotationCounter_deg + '°', 0, -this.length);

// draw a line from the center up
dynamicCtx.beginPath();
dynamicCtx.moveTo(0, 0);
dynamicCtx.lineTo(0, -this.length + 20);
dynamicCtx.stroke();

// rotate the canvas
console.log('rotating', currentRotation_rad, 'radians');
++rotationCounter_deg;
}
};

function drawDynamic() {
myDynamicObject.draw();
// you can add more dynamic objects and draw here
}

function animate() {
setInterval(function() {
// only need to redraw dynamic objects
drawDynamic();
}, INTERVAL);
}

animate(); // entry point for animated (dynamic) objects
})();
#canvasesdiv {
position: relative;
width: 400px;
height: 400px;
}
#dynamic {
position: absolute;
left: 0;
top: 0;
z-index: 1;
}
<div id="canvasesdiv">
<canvas id="dynamic" width=400 height=400>This text is displayed if your browser does not support HTML5 Canvas</canvas>
</div>

额外提示:您还可以合并 setTransform() 和第一个 translate():

dynamicCtx.setTransform(1, 0, 0, 1, 0, 0);
dynamicCtx.translate(dynamic.width / 2, dynamic.width / 2);

至:

dynamicCtx.setTransform(1, 0, 0, 1, dynamic.width * 0.5, dynamic.width * 0.5);

关于javascript - 调用 setTransform 后 Canvas 不旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37822199/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com