获取HTML Canvas中旋转矩形剑的坐标

获取html canvas中旋转矩形剑的坐标

本文旨在解决HTML Canvas中绘制旋转矩形剑,并获取其端点坐标的问题。通过分析现有代码,明确了剑的端点计算方式,并提供了修正后的sword.update()函数,确保剑随角色手臂旋转,同时提供了完整的可运行代码示例,方便开发者直接应用到自己的项目中。

在HTML Canvas中绘制旋转的矩形,特别是像剑这样的武器,涉及到坐标计算和角度旋转。以下是如何获取Canvas中旋转矩形剑的坐标并正确绘制它的步骤:

理解坐标系统

Canvas的坐标系统原点(0, 0)位于左上角。X轴正方向向右,Y轴正方向向下。所有坐标计算都基于这个原点。

关键代码分析与修正

原代码中sword.update()函数计算剑的端点坐标存在问题,导致剑的绘制不正确。正确的逻辑是:

立即学习“前端免费学习笔记(深入)”;

左手的位置是剑的一个端点。右手的位置是剑的另一个端点。剑的另外两个端点可以通过在右手位置的基础上,增加Player.swordLength来确定。

因此,需要修改sword.update()函数如下:

 update() {    this.draw();    this.Lx = LeftHand.x;    this.Ly = LeftHand.y;    this.Rx = RightHand.x;    this.Ry = RightHand.y;    this.Lsx = LeftHand.x;    this.Lsy = LeftHand.y;    this.Rsx = RightHand.x + Player.swordLength;    this.Rsy = RightHand.y + Player.swordLength;  }

这段代码的关键在于正确设置了Lsx、Lsy、Rsx和Rsy的值,确保剑的形状和旋转与手臂的运动保持一致。

完整代码示例

以下是修正后的完整代码示例,可以直接复制并运行:

var c = document.getElementById("canvas");var ctx = c.getContext("2d");c.width = window.innerWidth;c.height = window.innerHeight;var mouse = { x: c.width / 2, y: c.height / 2 };window.addEventListener("resize", function (event) {  c.width = window.innerWidth;  c.height = window.innerHeight;});window.addEventListener("mousemove", function (event) {  mouse.x = event.clientX;  mouse.y = event.clientY;});class player {  constructor(x, y, r, color, v) {    this.x = x;    this.y = y;    this.r = r;    this.v = v;    this.color = color;    this.swordLength = 200;  }  draw() {    ctx.beginPath();    ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);    ctx.fillStyle = this.color;    ctx.fill();    ctx.closePath();  }  update() {    this.draw();    var dy = mouse.y - this.y;    var dx = mouse.x - this.x;    const angle = Math.atan2(dy, dx);    var vx = Math.cos(angle) * this.v;    var vy = Math.sin(angle) * this.v;    if (Math.abs(vx) > Math.abs(dx)) {      vx = dx;    }    if (Math.abs(vy) > Math.abs(dy)) {      vy = dy;    }    this.x += vx;    this.y += vy;  }}class leftHand {  constructor(x, y, r, color) {    this.x = x;    this.y = y;    this.color = color;    this.angle = 0;    this.r = r;    this.Area = 40;  }  draw() {    ctx.beginPath();    ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);    ctx.fillStyle = this.color;    ctx.fill();    ctx.closePath();  }  update() {    this.draw();    this.x = Player.x + this.Area * Math.cos(this.angle / 180);    this.y = Player.y + this.Area * Math.sin(this.angle / 180);    this.angle += 30;  }}class rightHand {  constructor(x, y, r, color) {    this.x = x;    this.y = y;    this.color = color;    this.angle = 90;    this.r = r;    this.Area = 40;  }  draw() {    ctx.beginPath();    ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);    ctx.fillStyle = this.color;    ctx.fill();    ctx.closePath();  }  update() {    this.draw();    this.x = Player.x + this.Area * Math.cos(this.angle / 180);    this.y = Player.y + this.Area * Math.sin(this.angle / 180);    this.angle += 30;  }}class sword {  constructor(Lx, Ly, Rx, Ry, color, Lsx, Lsy, Rsx, Rsy) {    this.Lx = Lx;    this.Ly = Ly;    this.Rx = Rx;    this.Ry = Ry;    this.Lsx = Lsx;    this.Lsy = Lsy;    this.Rsx = Rsx;    this.Rsy = Rsy;    this.color = color;  }  draw() {    ctx.beginPath();    ctx.moveTo(this.Lx, this.Ly);    ctx.lineTo(this.Rx, this.Ry);    ctx.lineTo(this.Rsx, this.Rsy);    ctx.lineTo(this.Lsx, this.Lsy);    ctx.fillStyle = this.color;    ctx.fill();    ctx.closePath();  }  update() {    this.draw();    this.Lx = LeftHand.x;    this.Ly = LeftHand.y;    this.Rx = RightHand.x;    this.Ry = RightHand.y;    this.Lsx = LeftHand.x;    this.Lsy = LeftHand.y;    this.Rsx = RightHand.x + Player.swordLength;    this.Rsy = RightHand.y + Player.swordLength;  }}const Player = new player(c.width / 2, c.height / 2, 30, "blue", 10);const LeftHand = new leftHand(  c.width / 2 + 40 * Math.cos(0 / 180),  c.height / 2 + 40 * Math.sin(0 / 180),  10,  "red");const RightHand = new rightHand(  c.width / 2 + 40 * Math.cos(90 / 180),  c.height / 2 + 40 * Math.sin(90 / 180),  10,  "yellow");const Sword = new sword(  c.width / 2 + 40 * Math.cos(0 / 180),  c.height / 2 + 40 * Math.sin(0 / 180),  c.width / 2 + 40 * Math.cos(90 / 180),  c.height / 2 + 40 * Math.sin(90 / 180),  "black",  c.width / 2 + 40 * Math.cos(0 / 180),  c.height / 2 + 40 * Math.sin(0 / 180),  c.width / 2 + 40 * Math.cos(90 / 180),  c.height / 2 + 40 * Math.sin(90 / 180));function animate() {  requestAnimationFrame(animate);  ctx.clearRect(0, 0, c.width, c.height);  Player.update();  LeftHand.update();  RightHand.update();  Sword.update();}animate();

HTML文件:

        Canvas Sword      body {      margin: 0;      overflow: hidden; /* Prevent scrollbars */    }    canvas {      display: block; /* Remove extra space below canvas */    }          // JavaScript code from previous response goes here    var c = document.getElementById("canvas");    var ctx = c.getContext("2d");    c.width = window.innerWidth;    c.height = window.innerHeight;    var mouse = { x: c.width / 2, y: c.height / 2 };    window.addEventListener("resize", function (event) {      c.width = window.innerWidth;      c.height = window.innerHeight;    });    window.addEventListener("mousemove", function (event) {      mouse.x = event.clientX;      mouse.y = event.clientY;    });    class player {      constructor(x, y, r, color, v) {        this.x = x;        this.y = y;        this.r = r;        this.v = v;        this.color = color;        this.swordLength = 200;      }      draw() {        ctx.beginPath();        ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);        ctx.fillStyle = this.color;        ctx.fill();        ctx.closePath();      }      update() {        this.draw();        var dy = mouse.y - this.y;        var dx = mouse.x - this.x;        const angle = Math.atan2(dy, dx);        var vx = Math.cos(angle) * this.v;        var vy = Math.sin(angle) * this.v;        if (Math.abs(vx) > Math.abs(dx)) {          vx = dx;        }        if (Math.abs(vy) > Math.abs(dy)) {          vy = dy;        }        this.x += vx;        this.y += vy;      }    }    class leftHand {      constructor(x, y, r, color) {        this.x = x;        this.y = y;        this.color = color;        this.angle = 0;        this.r = r;        this.Area = 40;      }      draw() {        ctx.beginPath();        ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);        ctx.fillStyle = this.color;        ctx.fill();        ctx.closePath();      }      update() {        this.draw();        this.x = Player.x + this.Area * Math.cos(this.angle / 180);        this.y = Player.y + this.Area * Math.sin(this.angle / 180);        this.angle += 30;      }    }    class rightHand {      constructor(x, y, r, color) {        this.x = x;        this.y = y;        this.color = color;        this.angle = 90;        this.r = r;        this.Area = 40;      }      draw() {        ctx.beginPath();        ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);        ctx.fillStyle = this.color;        ctx.fill();        ctx.closePath();      }      update() {        this.draw();        this.x = Player.x + this.Area * Math.cos(this.angle / 180);        this.y = Player.y + this.Area * Math.sin(this.angle / 180);        this.angle += 30;      }    }    class sword {      constructor(Lx, Ly, Rx, Ry, color, Lsx, Lsy, Rsx, Rsy) {        this.Lx = Lx;        this.Ly = Ly;        this.Rx = Rx;        this.Ry = Ry;        this.Lsx = Lsx;        this.Lsy = Lsy;        this.Rsx = Rsx;        this.Rsy = Rsy;        this.color = color;      }      draw() {        ctx.beginPath();        ctx.moveTo(this.Lx, this.Ly);        ctx.lineTo(this.Rx, this.Ry);        ctx.lineTo(this.Rsx, this.Rsy);        ctx.lineTo(this.Lsx, this.Lsy);        ctx.fillStyle = this.color;        ctx.fill();        ctx.closePath();      }      update() {        this.draw();        this.Lx = LeftHand.x;        this.Ly = LeftHand.y;        this.Rx = RightHand.x;        this.Ry = RightHand.y;        this.Lsx = LeftHand.x;        this.Lsy = LeftHand.y;        this.Rsx = RightHand.x + Player.swordLength;        this.Rsy = RightHand.y + Player.swordLength;      }    }    const Player = new player(c.width / 2, c.height / 2, 30, "blue", 10);    const LeftHand = new leftHand(      c.width / 2 + 40 * Math.cos(0 / 180),      c.height / 2 + 40 * Math.sin(0 / 180),      10,      "red"    );    const RightHand = new rightHand(      c.width / 2 + 40 * Math.cos(90 / 180),      c.height / 2 + 40 * Math.sin(90 / 180),      10,      "yellow"    );    const Sword = new sword(      c.width / 2 + 40 * Math.cos(0 / 180),      c.height / 2 + 40 * Math.sin(0 / 180),      c.width / 2 + 40 * Math.cos(90 / 180),      c.height / 2 + 40 * Math.sin(90 / 180),      "black",      c.width / 2 + 40 * Math.cos(0 / 180),      c.height / 2 + 40 * Math.sin(0 / 180),      c.width / 2 + 40 * Math.cos(90 / 180),      c.height / 2 + 40 * Math.sin(90 / 180)    );    function animate() {      requestAnimationFrame(animate);      ctx.clearRect(0, 0, c.width, c.height);      Player.update();      LeftHand.update();      RightHand.update();      Sword.update();    }    animate();  

注意事项:

确保HTML文件中引入了JavaScript文件。Canvas元素的width和height属性应设置为窗口的尺寸,以充分利用屏幕空间。在animate()函数中使用requestAnimationFrame()可以实现更流畅的动画效果。

总结

通过理解Canvas坐标系统,并正确计算和更新剑的端点坐标,可以实现旋转矩形剑的绘制。关键在于sword.update()函数的修正,以及确保所有坐标计算都基于Canvas原点。 此外,代码示例中使用了面向对象的编程思想,将角色、手臂和剑都定义为类,使得代码结构更加清晰易懂。

以上就是获取HTML Canvas中旋转矩形剑的坐标的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1573331.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月22日 15:21:33
下一篇 2025年12月22日 15:21:48

相关推荐

  • 如何使用 scroll-behavior 属性实现元素scrollLeft变化时的平滑动画?

    如何实现元素scrollleft变化时的平滑动画效果? 在许多网页应用中,滚动容器的水平滚动条(scrollleft)需要频繁使用。为了让滚动动作更加自然,你希望给scrollleft的变化添加动画效果。 解决方案:scroll-behavior 属性 要实现scrollleft变化时的平滑动画效果…

    2025年12月24日
    000
  • 如何为滚动元素添加平滑过渡,使滚动条滑动时更自然流畅?

    给滚动元素平滑过渡 如何在滚动条属性(scrollleft)发生改变时为元素添加平滑的过渡效果? 解决方案:scroll-behavior 属性 为滚动容器设置 scroll-behavior 属性可以实现平滑滚动。 html 代码: click the button to slide right!…

    2025年12月24日
    500
  • 为什么设置 `overflow: hidden` 会导致 `inline-block` 元素错位?

    overflow 导致 inline-block 元素错位解析 当多个 inline-block 元素并列排列时,可能会出现错位显示的问题。这通常是由于其中一个元素设置了 overflow 属性引起的。 问题现象 在不设置 overflow 属性时,元素按预期显示在同一水平线上: 不设置 overf…

    2025年12月24日 好文分享
    400
  • 旋转长方形后,如何计算其相对于画布左上角的轴距?

    绘制长方形并旋转,计算旋转后轴距 在拥有 1920×1080 画布中,放置一个宽高为 200×20 的长方形,其坐标位于 (100, 100)。当以任意角度旋转长方形时,如何计算它相对于画布左上角的 x、y 轴距? 以下代码提供了一个计算旋转后长方形轴距的解决方案: const x = 200;co…

    2025年12月24日
    000
  • 旋转长方形后,如何计算它与画布左上角的xy轴距?

    旋转后长方形在画布上的xy轴距计算 在画布中添加一个长方形,并将其旋转任意角度,如何计算旋转后的长方形与画布左上角之间的xy轴距? 问题分解: 要计算旋转后长方形的xy轴距,需要考虑旋转对长方形宽高和位置的影响。首先,旋转会改变长方形的长和宽,其次,旋转会改变长方形的中心点位置。 求解方法: 计算旋…

    2025年12月24日
    000
  • 微信小程序文本省略后如何避免背景色溢出?

    去掉单行文本溢出多余背景色 在编写微信小程序时,如果希望文本超出宽度后省略显示并在末尾显示省略号,但同时还需要文本带有背景色,可能会遇到如下问题:文本末尾出现多余的背景色块。这是因为文本本身超出部分被省略并用省略号代替,但其背景色依然存在。 要解决这个问题,可以采用以下方法: 给 text 元素添加…

    2025年12月24日
    000
  • 旋转长方形后如何计算其在画布上的轴距?

    旋转长方形后计算轴距 假设长方形的宽、高分别为 200 和 20,初始坐标为 (100, 100),我们将它旋转一个任意角度。根据旋转矩阵公式,旋转后的新坐标 (x’, y’) 可以通过以下公式计算: x’ = x * cos(θ) – y * sin(θ)y’ = x * …

    2025年12月24日
    000
  • 如何让“元素跟随文本高度,而不是撑高父容器?

    如何让 元素跟随文本高度,而不是撑高父容器 在页面布局中,经常遇到父容器高度被子元素撑开的问题。在图例所示的案例中,父容器被较高的图片撑开,而文本的高度没有被考虑。本问答将提供纯css解决方案,让图片跟随文本高度,确保父容器的高度不会被图片影响。 解决方法 为了解决这个问题,需要将图片从文档流中脱离…

    2025年12月24日
    000
  • 如何计算旋转后长方形在画布上的轴距?

    旋转后长方形与画布轴距计算 在给定的画布中,有一个长方形,在随机旋转一定角度后,如何计算其在画布上的轴距,即距离左上角的距离? 以下提供一种计算长方形相对于画布左上角的新轴距的方法: const x = 200; // 初始 x 坐标const y = 90; // 初始 y 坐标const w =…

    2025年12月24日
    200
  • CSS元素设置em和transition后,为何载入页面无放大效果?

    css元素设置em和transition后,为何载入无放大效果 很多开发者在设置了em和transition后,却发现元素载入页面时无放大效果。本文将解答这一问题。 原问题:在视频演示中,将元素设置如下,载入页面会有放大效果。然而,在个人尝试中,并未出现该效果。这是由于macos和windows系统…

    2025年12月24日
    200
  • Flex 布局左右同高怎么实现?

    flex布局左右同高 在flex布局中,左右布局的元素高度不一致时,想要让边框延伸到最大高度,可以采用以下方法: 基于当前结构的方法: 给.rht和.lft盒子添加: .rht { height: min-content;} 这样可以使弹性盒子被子盒子内容撑开。 使用javascript获取.rht…

    2025年12月24日
    000
  • inline-block元素错位了,是为什么?

    inline-block元素错位背后的原因 inline-block元素是一种特殊类型的块级元素,它可以与其他元素行内排列。但是,在某些情况下,inline-block元素可能会出现错位显示的问题。 错位的原因 当inline-block元素设置了overflow:hidden属性时,它会影响元素的…

    2025年12月24日
    000
  • 为什么使用 inline-block 元素时会错位?

    inline-block 元素错位成因剖析 在使用 inline-block 元素时,可能会遇到它们错位显示的问题。如代码 demo 所示,当设置了 overflow 属性时,a 标签就会错位下沉,而未设置时却不会。 问题根源: overflow:hidden 属性影响了 inline-block …

    2025年12月24日
    000
  • 如何去除带有背景色的文本单行溢出时的多余背景色?

    带背景色的文字单行溢出处理:去除多余的背景色 当一个带有背景色的文本因单行溢出而被省略时,可能会出现最后一个背景色块多余的情况。针对这种情况,可以通过以下方式进行处理: 在示例代码中,问题在于当文本溢出时,overflow: hidden 属性会导致所有文本元素(包括最后一个)都隐藏。为了解决该问题…

    2025年12月24日
    000
  • 如何解决 CSS 中文本溢出时背景色也溢出的问题?

    文字单行溢出省略号时,去掉多余背景色的方法 在使用 css 中的 text-overflow: ellipsis 属性时,如果文本内容过长导致一行溢出,且文本带有背景色,溢出的部分也会保留背景色。但如果想要去掉最后多余的背景色,可以采用以下方法: 给 text 元素添加一个 display: inl…

    2025年12月24日
    200
  • 如何计算旋转后的长方形在画布上的 XY 轴距?

    旋转长方形后计算其画布xy轴距 在创建的画布上添加了一个长方形,并提供其宽、高和初始坐标。为了视觉化旋转效果,还提供了一些旋转特定角度后的图片。 问题是如何计算任意角度旋转后,这个长方形的xy轴距。这涉及到使用三角学来计算旋转后的坐标。 以下是一个 javascript 代码示例,用于计算旋转后长方…

    2025年12月24日
    000
  • 如何用CSS实现文本自动展开,并在超出两行后显示展开下箭头?

    CSS实现文本自动展开的难题 一段文本超出两行后自动溢出的效果,需要添加一个展开下箭头指示用户有隐藏内容。实现这一需求时,面临以下难题: 判断是否超过两行溢出取消省略号,用展开下箭头代替 解决思路:参考大佬文章 这个问题的解决方法,可以参考本站大佬的文章CSS 实现多行文本“展开收起”,该文章正是针…

    2025年12月24日
    000
  • 如何去除单行溢出文本中的冗余背景色?

    带背景色的文字单行溢出省略号,如何去除冗余背景色? 在使用 css 样式时,为单行溢出文本添加背景色可能会导致最后一行文本中的冗余背景色。为了解决这个问题,可以为文本元素添加额外的 css 样式: text { display: inline-block;} 添加这个样式后,文字截断将基于文本块进行…

    2025年12月24日
    000
  • 如何用 CSS 实现纵向文字溢出省略号?

    纵向文字溢出的省略号处理方案 对于纵向展示的文字,传统的横向溢出省略方案(使用 overflow: hidden; text-overflow: ellipsis;)不适用。若需在纵向展示时实现省略号,可考虑以下 css 解决方案: 垂直排版 通过将文字排版模式改为垂直,可以解决纵向溢出的问题。使用…

    2025年12月24日
    000
  • 前端代码辅助工具:如何选择最可靠的AI工具?

    前端代码辅助工具:可靠性探讨 对于前端工程师来说,在HTML、CSS和JavaScript开发中借助AI工具是司空见惯的事情。然而,并非所有工具都能提供同等的可靠性。 个性化需求 关于哪个AI工具最可靠,这个问题没有一刀切的答案。每个人的使用习惯和项目需求各不相同。以下是一些影响选择的重要因素: 立…

    2025年12月24日
    000

发表回复

登录后才能评论
关注微信