I've implemented a bouncing ball using html5 canvas, but can't figure out how to make the ball rotate at the same time

杀马特。学长 韩版系。学妹 提交于 2021-01-29 17:25:11

问题


I'm trying to implement a bouncing element around the edges of the viewport. I've successfully implemented the bouncing mechanism, but can't figure out how to make it rotate accordingly. Any help is appreciated!

Fiddle

function Ball (radius, color) {
  if (radius === undefined) { radius = 50; }
  this.x = 0;
  this.y = 0;
  this.radius = radius;
  this.vx = 0;
  this.vy = 0;
  this.rotation = 0;
  this.scaleX = 1;
  this.scaleY = 1;
  this.color = "#000";
}

Ball.prototype.draw = function (context) {
  context.save();
  context.translate(this.x, this.y);
  context.textAlign = "center";
  context.beginPath();
  context.arc(0, 0, this.radius, 0, (Math.PI * 2), true);
  context.closePath();
  context.fill();

  context.fillStyle = "#ffffff";x
  context.font="12px Arial";
  context.fillText("Lorem Ipsum",0,0);
  context.restore();
};

来源:https://stackoverflow.com/questions/65500718/ive-implemented-a-bouncing-ball-using-html5-canvas-but-cant-figure-out-how-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!