如何获取HTML Canvas中的旧生成元素的引用?

编程入门 行业动态 更新时间:2024-10-27 16:37:25
本文介绍了如何获取HTML Canvas中的旧生成元素的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 看看这个例子:

var c = document.getElementById(myCanvas); var ctx = c.getContext(2d); //第一个矩形创建 ctx.fillRect(20,20,150,100); //创建第二个矩形 ctx.fillRect(20,150,150,100); //创建第三个矩形 ctx.fillRect(20,300,150,100);

我在这里创建了三个矩形。创建第三个矩形后,我想旋转第一个矩形。如何获取第一个矩形的引用?

解决方案

画布只是一个哑像素的网格。它不明白它上面画的是什么形状。您的代码(或您的代码使用的库)必须跟踪您绘制的形状。

相反,它听起来像是您希望库创建场景图,如 EaselJS , Paper.js 或 KineticJS 。这些库将维护一个数据结构,用于跟踪画布上已绘制的形状,然后当您想操纵这些形状时,它们将重新绘制它们。

Take a look at this example:

var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); // First rectangle created ctx.fillRect(20,20,150,100); // Second rectangle created ctx.fillRect(20,150,150,100); // Third rectangle created ctx.fillRect(20,300,150,100);

I created three rectangles here. After creating third rectangle I want to rotate first rectangle. How do i get reference of first rectangle now?

解决方案

A canvas is just a dumb grid of pixels. It doesn't understand what shapes have been drawn on it. Your code (or a library that your code uses) must keep track of the shapes that you've drawn.

Instead, it sounds like you want a library to create a scene graph, like EaselJS, Paper.js, or KineticJS. These libraries will maintain a data structure that tracks what shapes have been drawn on the canvas, and they will then redraw them when you want to manipulate those shapes.

更多推荐

如何获取HTML Canvas中的旧生成元素的引用?

本文发布于:2023-11-30 06:29:09,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   HTML   Canvas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!