如何根据创建的画布形状创建粒子表面发射器? HTMLS 画布 JS
我有一个使用 html canvas 函数创建的形状(四分之一圆):
moveTo
LineTo
QuadraticCurveTo
我如何进行爆炸将形状分解为粒子,然后将它们返回形成圆形?
I have a shape (a quarter circle) that I've created using the html canvas function:
moveTo
LineTo
QuadraticCurveTo
How do I go about exploding the shape into particles and then return them to form a circle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会为你编写代码,因为这需要一些时间,而且我相信你可以在网上找到示例,但我会告诉你为了实现这样的事情你需要知道的理论。
document.createElement('canvas')
)。该画布必须至少与您的对象一样大。我假设它和你的物体一样大。我们将其称为tempCanvas
,它具有tempCtx
因此,要进行爆炸:
ctx.drawImage(tempCanvas, x, y)
,以便用户看到一些内容[20][30]
数组来对应。[x][y]
并使用ctx.drawImage(tempCanvas, x, y, 1, 1, newX, newY, 1, 1)< /code> 其中 x 和 y 与像素的
[x][y]
相同,并且 newX 和 newY 是使用向量计算的,并找到沿其直线的下一个点。结果将是在距离原始单击点稍远的位置绘制图像的每个像素。如果您继续一帧又一帧地执行此操作,就会看起来好像对象已经爆炸了。
无论如何,这就是总体想法。如果有任何不清楚的地方请告诉我。
注意 1:很可能您的普通画布与要爆炸的对象的大小不同。也许该对象放置在 100,100,因此您实际上单击了 110、115,而不是 10,15。为了简单起见,我省略了该偏移量。
I'm not going to write the code for you because it will take some time, and I'm sure you can find examples on the web, but I'll tell you the theory you need to know in order to make such a thing.
document.createElement('canvas')
) that will never be seen on the page. This canvas must be at least as large as your object. I'm going to assume it is exactly as large as your object. We'll call thistempCanvas
and it hastempCtx
So to do the explosion:
ctx.drawImage(tempCanvas, x, y)
so the user sees something[20][30]
to correspond.[x][y]
and usectx.drawImage(tempCanvas, x, y, 1, 1, newX, newY, 1, 1)
where x and y are the same as the pixel's[x][y]
and the newX and newY are calculated using the vector and finding what the next point would be along its line.The result will be each pixel of the image being drawn in a location that is slightly more away from the original click point. If you continue to do this frame after frame it will look as if the object has exploded.
That's the general idea, anyway. Let me know if any of it is unclear.
note 1: Most likely your normal canvas won't be the same size as the to-explode object. Maybe the object is placed at 100,100 so you really clicked on 110, 115 instead of 10,15. I'm omitting that offset just for the sake of simplicity.