简体   繁体   English

在同一画布中顺时针旋转一个轮子,逆时针旋转另一个轮子

[英]Rotate one wheel clockwise and another wheel anticlockwise in the same canvas

Hey I have two circular images in a canvas嘿,我在画布上有两个圆形图像

I wanted these two things:我想要这两件事:

  1. when we click the button wheel1 then I wanted rotate left wheel clockwise to 45 degrees and stop.当我们单击按钮轮 1 时,我想将左轮顺时针旋转到 45 度并停止。
  2. when we click the button wheel2 then I wanted rotate right wheel anti-clockwise to 45degrees and stop.当我们单击按钮轮 2 时,我想将右轮逆时针旋转到 45 度并停止。

I have been able to achieve the first one.我已经能够实现第一个。 But I have tried the whole day today and I am not able to figure out that how can I achieve the second point.但是我今天已经尝试了一整天,但我无法弄清楚如何实现第二点。 When I click on the second button the left wheel stops rotating and the right wheel is rotating in the whole canvas.当我点击第二个按钮时,左轮停止旋转,右轮在整个画布中旋转。

How can I rotate the right wheel anti clockwise to 45 degrees?Also is there any other efficient way to achieve the part in the question I have already achieved?我怎样才能将右轮逆时针旋转到 45 度? 还有没有其他有效的方法来实现我已经实现的问题中的部分?

  • Only load images once.只加载一次图像。 There is no need to load the image every time you need to draw it.每次需要绘制图像时,无需加载图像。 Also both wheels are the same image so you only need one copy of the image.此外,两个轮子都是相同的图像,因此您只需要图像的一个副本。

  • Use requestAnimationFrame to animate canvas content.使用requestAnimationFrame为画布内容设置动画。

  • Only get the canvas context once.只获取一次画布上下文。 There is no need to get it every frame.没有必要每帧都得到它。

  • Avoid changing the canvas size when there is no need to do so, as setting the canvas width or height, even if the same value will reset the whole 2D context state.避免在不需要时更改画布大小,因为设置画布宽度或高度,即使相同的值会重置整个 2D 上下文状态。

  • Don't repeat the same code.不要重复相同的代码。 The wheels are almost identical apart from position and rotation.除了位置和旋转之外,车轮几乎相同。 Define an object that describes a wheel and create that object for both wheels, setting only the properties that make them unique,.定义一个描述轮子的对象并为两个轮子创建该对象,仅设置使它们唯一的属性。

  • Do you really need jQuery?你真的需要jQuery吗? It is just bloat that will slow your page down and for all those additional lines of javascript you loaded you only called it twice.它只是膨胀,会减慢您的页面速度,并且对于您加载的所有这些额外的 javascript 行,您只调用了两次。 Using jQuery is no longer relevant, modern browser APIs are much faster and more powerful.使用 jQuery 不再重要,现代浏览器 API 更快、更强大。 Don't get left behind.不要掉队。

Example例子

Example of writing your code using the points outlined above.使用上述要点编写代码的示例。

 const wheelImg = new Image; wheelImg.src = "https://www.freeiconspng.com/uploads/car-wheel-png-image-free-download--car-wheel-png-image-free--11.png"; canvas.width = innerWidth - 30; canvas.height = innerHeight - 40; const ctx = canvas.getContext("2d"); const wheelScale = 0.4; // scales the wheels const rotateCount = 100; // frames (@60 per second) var activeWheelCount = 0; // to track if any wheels are active // when the image has loaded set up click events and draw the first frame wheelImg.addEventListener("load",() => { requestAnimationFrame(mainLoop) clockwiseBtn.addEventListener("click",() => wheels[0].start(Math.PI / 2)); antiClockwiseBtn.addEventListener("click",() => wheels[1].start(-Math.PI / 2)); } ,{once:true} ); // defines a single wheel const wheel = (x,y,rot) => ({ x,y,rot, rotTarget: rot, counter: 0, start(ang) { // command a wheel to turn if (!this.counter ) { // make sure not already turning this.counter = rotateCount; this.rotTarget += ang; if (!activeWheelCount) { requestAnimationFrame(mainLoop) } // start the // animation is no wheel // is active activeWheelCount ++; } }, update() { // update the wheel animation counter this.counter += this.counter > 0 ? -1 : 0; if (this.counter === 0) { this.rot = this.rotTarget } else { activeWheelCount += 1 } // count this wheel as an active wheel }, draw() { // draw the wheel const r = (1 - (this.counter / rotateCount)) * (this.rotTarget - this.rot) + this.rot; ctx.setTransform(wheelScale, 0, 0, wheelScale, this.x, this.y); ctx.rotate(r); ctx.drawImage(wheelImg, -wheelImg.width / 2, -wheelImg.height / 2); } }); const wheels = [wheel(90,80, 0), wheel(350,80,0)]; // create two wheels function mainLoop() { // animates wheels activeWheelCount = 0; ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.clearRect(0,0,ctx.canvas.width, ctx.canvas.height); wheels.forEach(wheel => wheel.update()); wheels.forEach(wheel => wheel.draw()); if (activeWheelCount) { requestAnimationFrame(mainLoop); } }
 #canvas { border:solid 1px black; position:absolute; top:30px; left:0px; }
 <canvas id="canvas"></canvas> <button id='clockwiseBtn'>Wheel1</button> <button id='antiClockwiseBtn'>Wheel2</button>

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

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