0

0

获取 HTML Canvas 中旋转剑的坐标

心靈之曲

心靈之曲

发布时间:2025-08-23 08:58:21

|

233人浏览过

|

来源于php中文网

原创

获取 html canvas 中旋转剑的坐标

本文旨在解决在 HTML Canvas 中绘制一个随角色旋转的剑的问题。通过分析角色手臂的位置,并结合剑的长度,计算出剑的四个端点坐标,从而实现剑随角色手臂旋转的效果。本文将提供详细的代码示例和解释,帮助开发者理解并实现这一功能。

在 HTML Canvas 中,要实现一个角色手持旋转的剑的效果,关键在于如何根据角色的手臂位置动态计算出剑的端点坐标。以下将详细讲解如何实现这个效果。

确定剑的端点

首先,我们需要确定剑的四个端点:左手起始点 (Lx, Ly),右手起始点 (Rx, Ry),左手延伸点 (Lsx, Lsy),右手延伸点 (Rsx, Rsy)。 左手和右手起始点分别对应角色左手和右手的位置,而延伸点则基于起始点加上剑的长度计算得出。

代码实现

在原代码的基础上,我们需要修改 sword 类的 update() 方法,以正确计算剑的端点坐标。

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

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;
  }
}

在 update() 方法中,我们首先将 Lx 和 Ly 设置为 LeftHand.x 和 LeftHand.y,将 Rx 和 Ry 设置为 RightHand.x 和 RightHand.y。 然后,Lsx 和 Lsy 设置为 LeftHand.x 和 LeftHand.y,Rsx 设置为 RightHand.x + Player.swordLength,Rsy 设置为 RightHand.y + Player.swordLength。

NameGPT名称生成器
NameGPT名称生成器

免费AI公司名称生成器,AI在线生成企业名称,注册公司名称起名大全。

下载

完整代码示例

以下是修改后的完整代码示例:

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文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Canvas Sword</title>
    <style>
        body {
            margin: 0;
            overflow: hidden; /* Prevent scrollbars */
        }
        canvas {
            display: block; /* Remove extra space below canvas */
        }
    </style>
</head>
<body>
    <canvas id="canvas"></canvas>
    <script src="canvas.js"></script>
</body>
</html>

注意事项:

  • 确保 HTML 文件正确引用 JavaScript 文件。
  • 根据实际需求调整角色和剑的属性,例如颜色、长度等。
  • 可以进一步优化代码,例如使用更高效的算法来计算坐标,或者添加更多的动画效果。

总结

通过修改 sword 类的 update() 方法,我们可以根据角色手臂的位置动态计算出剑的端点坐标,从而实现剑随角色手臂旋转的效果。 关键在于理解如何利用左右手的位置和剑的长度来确定剑的四个端点。 这个方法可以应用于更复杂的动画效果中,例如添加剑的攻击动作等。

相关文章

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
html5动画制作有哪些制作方法
html5动画制作有哪些制作方法

html5动画制作方法有使用CSS3动画、使用JavaScript动画库、使用HTML5 Canvas等。想了解更多html5动画制作方法相关内容,可以阅读本专题下面的文章。

550

2023.10.23

页面置换算法
页面置换算法

页面置换算法是操作系统中用来决定在内存中哪些页面应该被换出以便为新的页面提供空间的算法。本专题为大家提供页面置换算法的相关文章,大家可以免费体验。

504

2023.08.14

c++ 字符处理
c++ 字符处理

本专题整合了c++字符处理教程、字符串处理函数相关内容,阅读专题下面的文章了解更多详细内容。

0

2026.03.17

minimax视频生成教程汇总
minimax视频生成教程汇总

本专题整合了minimax生成视频相关教程,阅读下面的文章了解更多详细操作。

0

2026.03.17

c++ 读取二进制文件
c++ 读取二进制文件

本专题整合了c++读取二进制文件相关内容与教程,阅读专题下面的文章了解更多详细操作。

0

2026.03.17

c++ 全局变量
c++ 全局变量

本专题整合了c++全局变量的使用、定义、作用域等等内容,阅读专题下面的文章了解更多详细内容。

0

2026.03.17

c++ 全局变量
c++ 全局变量

本专题整合了c++全局变量的使用、定义、作用域等等内容,阅读专题下面的文章了解更多详细内容。

0

2026.03.17

Nginx跨平台安装实操指南:Windows、macOS与Linux环境快速搭建
Nginx跨平台安装实操指南:Windows、macOS与Linux环境快速搭建

本指南详解Nginx在Windows、macOS及Linux系统的安装全流程。涵盖官方包解压、Homebrew一键部署、APT/YUM源配置及Docker容器化方案。无论新手或开发者,均可快速搭建运行环境,掌握跨平台核心指令,为后续配置与调优奠定坚实基础。

11

2026.03.16

chatgpt使用指南
chatgpt使用指南

本专题整合了chatgpt使用教程、新手使用说明等等相关内容,阅读专题下面的文章了解更多详细内容。

22

2026.03.16

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
誉天教育RHCE视频教程
誉天教育RHCE视频教程

共9课时 | 1.5万人学习

尚观Linux RHCE视频教程(二)
尚观Linux RHCE视频教程(二)

共34课时 | 6.1万人学习

尚观RHCE视频教程(一)
尚观RHCE视频教程(一)

共28课时 | 4.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号