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

How do I get reference of old generated elements in HTML Canvas?(如何在 HTML Canvas 中获取旧生成元素的引用?)
本文介绍了如何在 HTML Canvas 中获取旧生成元素的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这个例子:

  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.

相反,听起来您想要一个库来创建 场景图,例如 EaselJS, Paper.js 或 KineticJS.这些库将维护一个数据结构,用于跟踪在画布上绘制了哪些形状,然后在您想要操作这些形状时重新绘制它们.

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 中获取旧生成元素的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)