html5-canvas 在线移动对象

2023-12-02

我想在斜线上移动物体。我已经给出了我的代码。在我的第三个 div 的代码中,当我移动滑块时,我在这条线上画了一条斜线,我想移动一个对象。我在第一个分区中正在做类似的事情。我在曲线上移动物体的地方。我正在寻找一些函数,在其中我将提供点并且对象将跟随点。这是我的代码。该代码仅适用于 chrome,因为我试图使其仅适用于 safari 和 chrome 浏览器。

        <!DOCTYPE HTML>
            <html>
            <head>

   <style type="text/css">
        .wrapper {
            margin: 0 auto;
            width: 1000px;
        }
        .canHdr {
           float: left;
           width: 450px;
               height: 400px;
               border: 1px solid red;
    }
  </style>

    </head>
    <body>
    <form>
    <!-- wrapper -->
    <div class="wrapper">

        <!-- canHdr -->
        <div id="canHdr" class="canHdr" >

            <p>
                This is my 1st div with bezier curve the curve is getting drawn as slider moves and also a ball in moving on that . 
            </p>

            <div class="canOuterHdr" >
                <canvas id="myCanvas1" width="300" height="195" style="position: relative;">
                    [No canvas support]
                </canvas>

            </div>

            <div id="slider1" class="newBg">
                <input id="slide1" type="range" min="0" max="100" step="1" value="0" onchange="counterSlider('slide1');" />
            </div>

        </div>
        <!--/ canHdr -->
        <!-- canHdr2 -->
        <div id="canHdr2" class="canHdr" >

            <p>
                This is my 2nd div
            </p>

            <div class="canOuterHdr" >
                <canvas id="myCanvas2" width="300" height="195" style="position: relative;">
                    [No canvas support]
                </canvas>

            </div>

            <div id="slider2" class="newBg">
                <input id="slide2" type="range" min="0" max="100" step="1" value="0" onchange="counterSlider('slide2');" />
            </div>

        </div>
        <!-- canHdr2 -->
        <!-- canHdr3 -->
        <div id="canHdr3" class="canHdr" >
            <p>
                This is my 3rd div with slanting line. I want to move a ball on this line when I move the slider. So as the line increases ball will also move on the line.
            </p>

            <div class="canOuterHdr" >
                <canvas id="myCanvas3" width="300" height="195" style="position: relative;">
                    [No canvas support]
                </canvas>

            </div>

            <div id="slider3" class="newBg">
                <input id="slide3" type="range" min="0" max="100" step="1" value="0" onchange=" drawSlopeCurve2('slide3','100'); " />
            </div>

        </div>
        <!--/ canHdr3 -->
        <!-- canHdr4 -->
        <div id="canHdr4" class="canHdr" >

            <p>
                This is my 4th div with slanting line. I want to move a ball on this line when I move the slider. So as the line increases ball will also move on the line.
            </p>

            <div class="canOuterHdr" >
                <canvas id="myCanvas4" width="300" height="195" style="position: relative;">
                    [No canvas support]
                </canvas>

            </div>

            <div id="slider4" class="newBg">
                <input id="slide4" type="range" min="0" max="100" step="1" value="0" onchange=" drawSlopeCurve1('slide4','100'); " />
            </div>

        </div>
        <!--/ canHdr4 -->

    </div>
    <!-- /wrapper -->

    <script type="text/javascript">
        function counterSlider(sID) {

            var slideVal = document.getElementById(sID).value;
            /*if (maxValue ==100){

             slideVal=slideVal/100;
             }*/
            slideVal = slideVal / 100;
            var position = slideVal;

            var startPt = {
                x : 18.8,
                y : 45
            };
            var controlPt = {
                x : 28,
                y : 160
            };
            var endPt = {
                x : 228,
                y : 165
            };
            var startPt2 = {
                x : 20,
                y : 75
            };
            var controlPt2 = {
                x : 28,
                y : 160
            };
            var endPt2 = {
                x : 228,
                y : 165
            };

            if (slideVal == 0) {

                erase('myCanvas2');
                erase('myCanvas3');
                erase('myCanvas4');
                //newSprite('myCanvas1b', 18.8, 45);

                drawBezier2('myCanvas1', new Array({
                    x : 18.8,
                    y : 45
                }, {
                    x : 28,
                    y : 160
                }, {
                    x : 228,
                    y : 165
                }), slideVal);
                drawBezier2('myCanvas2', new Array({
                    x : 20,
                    y : 75
                }, {
                    x : 28,
                    y : 160
                }, {
                    x : 228,
                    y : 165
                }), slideVal);

            } else if (slideVal > 0 && slideVal <= 34) {

                erase('myCanvas1');
                //erase('myCanvas1b');
                erase('myCanvas2');
                erase('myCanvas3');
                erase('myCanvas4');

                drawBezier2('myCanvas1', new Array({
                    x : 18.8,
                    y : 45
                }, {
                    x : 28,
                    y : 160
                }, {
                    x : 228,
                    y : 165
                }), slideVal);
                drawBezier2('myCanvas2', new Array({
                    x : 20,
                    y : 75
                }, {
                    x : 28,
                    y : 160
                }, {
                    x : 228,
                    y : 165
                }), slideVal);

                drawNextPoint('myCanvas1', startPt, controlPt, endPt, position);
                drawNextPoint('myCanvas2', startPt2, controlPt2, endPt2, position);

            } else if (slideVal > 34 && slideVal <= 67) {

                erase('myCanvas1');

                erase('myCanvas2');
                erase('myCanvas3');
                erase('myCanvas4');

                drawBezier2('myCanvas1', new Array({
                    x : 18.8,
                    y : 45
                }, {
                    x : 28,
                    y : 160
                }, {
                    x : 228,
                    y : 165
                }), slideVal);
                drawBezier2('myCanvas2', new Array({
                    x : 20,
                    y : 75
                }, {
                    x : 28,
                    y : 160
                }, {
                    x : 228,
                    y : 165
                }), slideVal);

                drawNextPoint('myCanvas1', startPt, controlPt, endPt, position);
                drawNextPoint('myCanvas2', startPt2, controlPt2, endPt2, position);

            } else if (slideVal > 67 && slideVal <= 100) {

                erase('myCanvas1');

                erase('myCanvas2');
                erase('myCanvas3');
                erase('myCanvas4');

                drawBezier2('myCanvas1', new Array({
                    x : 18.8,
                    y : 45
                }, {
                    x : 28,
                    y : 160
                }, {
                    x : 228,
                    y : 165
                }), slideVal);
                drawBezier2('myCanvas2', new Array({
                    x : 20,
                    y : 75
                }, {
                    x : 28,
                    y : 160
                }, {
                    x : 228,
                    y : 165
                }), slideVal);

                drawNextPoint('myCanvas1', startPt, controlPt, endPt, position);
                drawNextPoint('myCanvas2', startPt2, controlPt2, endPt2, position);

            }
        }

        function erase(canvasId) {

            var canvas = document.getElementById(canvasId);
            var context = canvas.getContext("2d");
            context.beginPath();
            context.clearRect(0, 0, canvas.width, canvas.height);
            canvas.width = canvas.width;

        }


        /**********for backgroundImage********************/

        function _getQBezierValue(t, p1, p2, p3) {
            var iT = 1 - t;
            return iT * iT * p1 + 2 * iT * t * p2 + t * t * p3;

        }

        function getQuadraticCurvePoint(startX, startY, cpX, cpY, endX, endY, position) {
            return {
                x : _getQBezierValue(position, startX, cpX, endX),
                y : _getQBezierValue(position, startY, cpY, endY)
            };
        }

        function drawNextPoint(canId, startPt, controlPt, endPt, position) {
            var pt = getQuadraticCurvePoint(startPt.x, startPt.y, controlPt.x, controlPt.y, endPt.x, endPt.y, position);
            position = (position + 0.006) % 1.0;
            var canvas = document.getElementById(canId);
            var ctx = canvas.getContext('2d');
            //ctx.globalCompositeOperation = 'source-atop';
            //ctx.globalCompositeOperation = "destination-over";
            ctx.beginPath();
            ctx.fillStyle = "#0077c1";
            ctx.arc(pt.x, pt.y, 6, 0, Math.PI * 2, true);
            ctx.closePath();
            ctx.fill();
        }

        function newSprite(canId, mvx, mvy) {
            var canvas = document.getElementById(canId);
            var ctx = canvas.getContext('2d');
            ctx.globalCompositeOperation = 'source-atop';
            //ctx.globalCompositeOperation = "destination-over";
            ctx.beginPath();
            ctx.fillStyle = "#0077c1";
            ctx.arc(mvx, mvy, 6, 0, Math.PI * 2, true);
            ctx.closePath();
            ctx.fill();
        }

        function drawBezier2(canId, points, slideVal) {

            var canvas = document.getElementById(canId);

            var context = canvas.getContext("2d");
            //context.globalCompositeOperation = 'source-atop';
            //context.strokeStyle = "rgb(113, 113, 213)";
            context.strokeStyle = "#000";
            context.lineWidth = 0.6;
            context.beginPath();
            // Label end points
            //context.fillStyle = "rgb(0, 0, 0)";
            // Draw spline segemnts
            context.moveTo(points[0].x, points[0].y);
            for (var t = 0; t <= slideVal; t += 0.1) {
                context.lineTo(Math.pow(1 - t, 2) * points[0].x + 2 * (1 - t) * t * points[1].x + Math.pow(t, 2) * points[2].x, Math.pow(1 - t, 2) * points[0].y + 2 * (1 - t) * t * points[1].y + Math.pow(t, 2) * points[2].y);
            }

            // Stroke path
            context.stroke();
        }

        function drawSlopeCurve1(sID, maxValue) {
            // erase('canvasTwo');

            var canId = 'myCanvas4';
            var slideVal = parseInt(document.getElementById(sID).value);
            var canvas = document.getElementById(canId);
            var context = canvas.getContext('2d');
            canvas.width = canvas.width;
            //line end points
            x1 = 16;
            y1 = 170;
            x2 = 200;
            y2 = 80;

            //get slope (rise over run)
            var m = (y2 - y1) / (x2 - x1);
            //get y-intercept
            var b = y1 - (m * x1);
            //get distance between the two points
            var distance = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
            //get new x and y values
            var x = x1 + parseInt(distance / maxValue * slideVal);
            var y = parseInt(m * x + b);

            context.beginPath();
            context.moveTo(x1, y1);
            context.lineTo(x, y);
            context.lineWidth = 0.6;
            context.stroke();
        }

        function drawSlopeCurve2(sID, maxValue) {
            // erase('canvasTwo');

            var canId = 'myCanvas3';
            var slideVal = parseInt(document.getElementById(sID).value);
            var canvas = document.getElementById(canId);
            var context = canvas.getContext('2d');
            canvas.width = canvas.width;
            //line end points
            x1 = 16;
            y1 = 170;
            x2 = 160;
            y2 = 72;

            //get slope (rise over run)
            var m = (y2 - y1) / (x2 - x1);
            //get y-intercept
            var b = y1 - (m * x1);
            //get distance between the two points
            var distance = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
            //get new x and y values
            var x = x1 + parseInt(distance / maxValue * slideVal);
            var y = parseInt(m * x + b);

            context.beginPath();
            context.moveTo(x1, y1);
            context.lineTo(x, y);
            context.lineWidth = 0.6;
            context.stroke();
        }

    </script>
        </form>
    </body>
    </html>

提前致谢。我的 jsfiddle 链接:http://jsfiddle.net/g7hWD/1/


您需要在函数的最后添加绘图代码drawSlopeCurve1() and drawSlopeCurve2()。最简单的方法就是修复函数newSprite()首先然后使用它(以避免一遍又一遍地复制相同的代码块)。

功能中newSprite():

// Change that:
ctx.globalCompositeOperation = 'source-atop';

// To this:
ctx.globalCompositeOperation = "source-over";

(有关更多详细信息globalCompositeOperation see here.)

在函数的最后drawSlopeCurve1/2():

// Append this:
newSprite(canId, x, y);

另请参阅此修改演示.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

html5-canvas 在线移动对象 的相关文章

  • Angular - 如何从 DOM 中删除我使用过 $compile 的元素?

    我需要的是两个 ng views 的功能 因为我不能 我想更改某些内容的innerHTML 并编译它 我遇到的问题是 当我再次更改内容时 我可以编译 但是 Angular 是否会自行删除绑定 或者我必须手动执行此操作 如果是这样 怎么办 编
  • 如何使用 JavaScript 创建链接?

    我有一个标题字符串和一个链接字符串 我不知道如何将两者放在一起以使用 JavaScript 在页面上创建链接 任何帮助表示赞赏 我试图解决这个问题的原因是因为我有一个 RSS 源并且有一个标题和 URL 列表 我想将标题链接到 URL 以使
  • 以编程方式填写reactjs表单

    我正在编写一个用户脚本 但无法填写由reactjs制作的表单 我的代码 document querySelector id username value email protected cdn cgi l email protection
  • Chrome 中的性能问题

    我目前正在从事一个相对较大的项目 使用 AngularJs 构建 应用程序的一部分是一个表单 您可以向其中添加任意数量的页面 不幸的是 添加了很多不必要的垃圾 即表示表单模型的对象可能会变得非常大 在某些时候 Chrome 基本上无法处理它
  • 如何将内联 JavaScript 与 Express/Node.js 中动态生成的内容分开?

    对于具有几年 Web 开发经验但没有找到答案的人来说 这是一个有点菜鸟的问题程序员堆栈交换 or Google 我决定在这里问一下 我在用Express网络框架Node js 但这个问题并不特定于任何 Web 框架或编程语言 以下是从数据库
  • 摩卡 - Chai Karma“套件未定义”

    我对 jscript tdd 很陌生 遇到了问题 希望有人能告诉我我在做什么 在浏览器中运行测试 通过 HTML 文件 一切正常 通过节点和业力运行它们我得到以下异常 我想在 node js 主机的 karma 中使用 Mocha 和 Ch
  • Bootstrap按钮加载+Ajax

    我正在使用 Twitter Bootstrap 的按钮加载状态 http twitter github com bootstrap javascript html buttons http twitter github com bootst
  • 有没有办法使用 Rspec/Capybara/Selenium 将 javascript console.errors 打印到终端?

    当我运行 rspec 时 是否可以让 capybara selenium 向 rspec 报告任何 javascript console errors 和其他异常 我有一大堆测试失败 但当我手动测试它时 我的应用程序正在运行 如果不知道仅在
  • 使用 CSS 或 Javascript 填充动画

    我只是想知道是否可以使用 CSS 或 javascript 创建填充动画 基本上我想创建一个填充动画 如下图所示 http i40 tinypic com eit6ia png http i40 tinypic com eit6ia png
  • 如何解决 Typescript 构建中的错误“找不到模块 'jquery'”

    我目前在 ts 文件的顶部有这个import require jquery 我这样做是因为我试图在我的打字稿文件中使用 jquery 但我似乎无法编译它 因为它返回标题中所述的错误 我正在使用 ASP NET CORE 脚本文件夹 tsco
  • Javascript split 不是一个函数

    嘿朋友们 我正在使用 javascript sdk 通过 jQuery facebook 多朋友选择器在用户朋友墙上发布信息 但是我收到此错误friendId split 不是函数 这是我的代码 function recommendToFr
  • window.location 和 location.href 之间的区别

    我对之间的区别感到困惑window location and location href 两者似乎都以相同的方式行事 有什么不同 window location是一个对象 它保存有关当前文档位置的所有信息 主机 href 端口 协议等 lo
  • Javascript 假值(null、未定义、false、空字符串:“”或 '' 和 0)和比较(==)运算符 [重复]

    这个问题在这里已经有答案了 当我使用任何一个值时 null undefined false 0 in a if陈述 它总是被评估为谬误 false 另外 这些值的否定 null undefined false 0 in a if语句总是被评
  • Firebase 函数 onWrite 未被调用

    我正在尝试使用 Firebase 函数实现一个触发器 该触发器会复制数据库中的一些数据 我想观看所有添加的内容votes user vote 结构为 我尝试的代码是 const functions require firebase func
  • 从数据库检查数据的异步解决方案各种循环子句

    我想要做的是异步检查数据库并从中获取结果 在我的应用程序中我试图实现Asynchronously将此步骤解决为 从数据库中检查手机号码JsonArray循环子句的种类 Create JsonArray从结果 打印创建的数组 我学到了足够多的
  • 日期出现奇怪的错误,“未捕获非法访问”

    所以我试图找到最新的DateJavascript 可以处理 我把它减少到 9 月 275760 并增加了我开始捕获未捕获的天数illegal access例外new Date 09 24 275760 to new Date 10 13 2
  • Javascript - 水波纹效果

    我需要 JS 上的脚本 它将以 水波纹 样式更改 images html 抱歉 6MB GIF 文件 http fcuunited ru temp listening2 gif http fcunited ru temp listening
  • 带参数的事件监听器

    我想将参数传递给 JavaScript 中的事件侦听器 我已经找到了解决方案 但我无法理解它们为什么或如何工作以及为什么其他解决方案不起作用 我有 C C 背景 但是 Javascript 函数的执行有很大不同 您能否帮助我理解以下示例如何
  • 如何用另一个响应替换窗口的 URL 哈希?

    我正在尝试使用替换方法更改哈希 URL document location hash 但它不起作用 function var anchor document location hash this returns me a string va
  • 使用 MongoDB 和 Nodejs 插入和查询日期

    我需要一些帮助在 mongodb 和 nodejs 中按日期查找记录 我将日期添加到抓取脚本中的 json 对象 如下所示 jsonObj last updated new Date 该对象被插入到 mongodb 中 我可以看到如下 la

随机推荐

  • 随机运动pygame

    我正在尝试制作一个简单的生活模拟器 我需要 细胞 在屏幕上几乎随机移动 有一些规则 但问题是 一段时间后 它们往往会聚集在屏幕的左上角 我尝试改变很多事情 比如完全跳过规则并让它们完全随机移动 但它们仍然聚集在一起 我的代码中是否存在一些明
  • 使用jwplayer 6.11视频无法在ipad上播放

    我们使用 JW Player 6 11 来播放 mp4 和 flv 视频 但无法正常工作 ios7 iPhone 我该如何解决 iPad 的这个问题 function playvideo jwplayer mediaplayer setup
  • Android 上的 Snackbar 无需更改主题

    当我在 Android 上创建小吃栏时 出现以下错误 java lang IllegalStateException 您需要在此活动中使用 Theme AppCompat 主题 或后代 我不想将活动的主题更改为 AppCompat 有没有办
  • 从相对路径中的文件加载 C# 中的图片框图像

    我在 Windows 窗体解决方案中有一个图片框图像 用户从数据库中选择一个项目后 我想将图像加载到此图片框中 图像的文件名将来自数据库 并且所有图像必须存储在应用程序文件夹 Images 的子文件夹中 我不想在我的解决方案中包含所有这些
  • Android:如何查找屏幕的宽度和高度?

    我试图找到屏幕的宽度和高度 但我尝试过的方法都不适用于我创建的类 我的代码如下 有谁知道如何找到它 我无法使用下面尝试的方式 因为 getWidth 已被弃用 public class Crate public int acrossCrat
  • 在 LINQ 查询中的模型值中使用列表

    我正处于 ASP NET MVC 开发的非常基础的阶段 因此 有时我很难使用简单的 LINQ 查询来工作 设想 我有一个页面有一些Image并由用户对该图像发表评论 就像 Facebook 上包含用户评论的帖子一样 因此 我从文本区域保存这
  • git merge squash - 当我想要的只是我压扁的分支的更改时冲突解决

    I have github当主分支达到某种可接受的状态时 我将其推送到 github 的分支 已经这样做过一次 为此我做了 MrD MRSD c Dropbox eclipse workspaces android AndroidMonit
  • 为 C++ 指针集定义运算符<

    我正在尝试编写 C STL 集 它保留指向我的自定义类的指针 但我不明白当我使用指针而不是类的对象时如何重载 我读到 如果我们重载 set 用于比较的 当您使用operator 函数创建一个结构并将其传递给集合时 我遇到了这个技巧 例如se
  • Tensorflowpartial_run()“在执行部分运行之前必须运行‘setup’!”尽管已被设立

    我正在使用tensorflow的partial run 方法围绕运行子图构建概念验证 而无需重新计算 目前我有一个简单的小 python 脚本 见下文 它应该将两个占位符值相乘并加 1 作为部分图运行 此操作有效一次 然后失败并出现错误 t
  • 如何分析 goroutine 的数量

    基本上我想知道我的程序是否随着时间的推移而泄漏 goroutine 所以 我想看看随着时间的推移有多少 goroutine 正在运行 有什么办法可以通过pprof 我已经搞定了go tool pprof http localhost 888
  • 如何在 docker 容器上设置 ulimit / 文件描述符 镜像标签为 phusion/baseimage-docker

    我需要在 docker 容器上正确设置文件描述符限制 我使用 ssh 连接到容器 https github com phusion baseimage docker 已经尝试过 编辑limits conf容器忽略这个文件 新贵程序位于htt
  • 如何在空手道框架中编辑配置的标头

    在我的框架中 我有 headers js 文件 我使用命令在每个功能文件的背景上调用该 js 文件 configure headers read headers js 这按预期工作 某些情况下我需要更改 client id 值 例如 hea
  • SSLHandshakeException:收到致命警报:Java 6 -> 8 升级后握手失败

    我们最近将一个项目从 Java 6 更新到了 Java 8 现在我们在 SSL 握手方面遇到了障碍 服务层使用客户端请求和接收来自第三方应用程序的调用 在服务层 密钥库初始化为 System setProperty javax net ss
  • 从数据库中写入和读取最新(按日期)的行

    我对下面的代码有三个问题 基本上我正在尝试以 currentdate count 格式写入行 继续将行日期附加到同一个 csv 文件中 读取最近的 5 个条目并打印它 目前我尝试了以下操作并遇到以下错误 1 如何以当前日期格式写入一行 计数
  • Android 上的 Proguard 与 OrmLite

    我应该如何在 Android 上将 proguard 与 ormlite 库一起使用 尝试这个 keep class com j256 keepclassmembers class com j256 keep enum com j256 k
  • 第 n 个子元素选择错误的元素

    对于这个特定的站点 当我通过 CSS 或 jQuery 使用 nth child 时 nth child 选择器捕获了错误的元素 我在调用的选择器之前有一个孩子 home article nth child 3 captures 2nd c
  • 评估 FeatherJS 身份验证需求

    我和我的同事想构建一个聊天应用程序 ReactJS NodeJS 我们一直在寻找最好的框架来实现这一点 FeathersJS 似乎无疑是最稳定且功能丰富的 socket io 包装器 然而 由于我们希望允许我们的应用程序扩展 因此我们决定将
  • 如何检测字符串列表中的公共子字符串

    给定一组字符串 例如 EFgreen EFgrey EntireS1 EntireS2 J27RedP1 J27GreenP1 J27RedP2 J27GreenP2 JournalP1Black JournalP1Blue Journal
  • 尝试改变返回 Iterator 的闭包内的状态时,出现 Rust 错误“无法推断借用表达式的适当生命周期”

    我正在尝试学习 Rust 并在尝试模拟嵌套 Python 生成器时遇到了与生命周期相关的问题 正如编译器所报告的 问题在于由闭包改变的值的生命周期 代码的关键是 flat mapping 一个闭包 它调用一个函数 该函数在其返回的迭代器中改
  • html5-canvas 在线移动对象

    我想在斜线上移动物体 我已经给出了我的代码 在我的第三个 div 的代码中 当我移动滑块时 我在这条线上画了一条斜线 我想移动一个对象 我在第一个分区中正在做类似的事情 我在曲线上移动物体的地方 我正在寻找一些函数 在其中我将提供点并且对象