简体   繁体   English

贝塞尔曲线动态创建?

[英]bezier curve creation dynamically?

I am trying to achieve grass like bend on a straight line created by using Cubic bezier curve .I have created a straight line using Bezier curve and now i want to bend it from top.But the problem I am facing is I am not able to do it dynamically .The moment I use mousemove for creating the bend,series of curves appear giving the canvas a paint like look which is somthing I dont want.I just want a single curved line.My question is how do I clear the previous lines that have been drawn and restore the latest bended curve?? 我试图通过使用Cubic bezier曲线在直线上实现弯曲。我使用Bezier曲线创建了一条直线,现在我想从顶部弯曲它。但我面临的问题是我无法做动态 .The目前我使用鼠标移动创建的弯,一系列的曲线出现让画布般的外观油漆是财产以后我不want.I只想要一个可以弯曲line.My问题是,我该如何清除之前的线已绘制并恢复最新的弯曲曲线? My CODE: here is my code if you want to have a look at it 我的代码: 如果你想查看它,这是我的代码

Create two canvases stacked on top of each other: 创建两个堆叠在彼此之上的画布:

The first one containing the static content, the other only the dynamic content. 第一个包含静态内容,另一个只包含动态内容。

This way you will only need to be concerned about clearing the area the grass was drawn in (and this is much faster too as there is no need to redraw static all the time). 通过这种方式,您只需要关注清除草被吸入的区域(这也更快,因为不需要一直重绘静态)。

Place the two canvases inside a div which have position set to relative, then place the canvases using position set to absolute. 将两个画布放在div中,其位置设置为相对,然后使用设置为绝对的位置放置画布。

Now you can make a function to draw the static content (you will need to redraw it if the browser window gets resized etc.). 现在你可以创建一个绘制静态内容的函数(如果浏览器窗口调整大小等,你需要重绘它)。

Record the area the grass is drawn within and clear only this (for each grass) in the next frame. 记录草被绘制的区域,并在下一帧中仅清除(对于每个草)。

If this last is too complicated, just use a simple clear on the "dynamic" canvas: 如果这最后太复杂,只需在“动态”画布上使用简单的清除:

ctxDyn.clear(0, 0, canvasDyn.width, canvasDyn.height);

This will also preserve transparency of this canvas, or "layer" if you like. 如果您愿意,这也将保留此画布或“图层”的透明度。

Use requestAnimationFrame to do the actual animation. 使用requestAnimationFrame来执行实际动画。

var isPlaying = true;

function animate() {

    ctxDyn.clear(0, 0, canvasDyn.width, canvasDyn.height);

    drawGrass(); //function to draw the actual grass

    if (isPlaying) requestAnimationFrame(animate);
}

animate(); // start

The isPlaying is a flag you can toggle with a button on screen or something similar, to be able to stop the animation. isPlaying是一个标志,您可以使用屏幕上的按钮或类似的东西切换,以便能够停止动画。 See the link for requestAnimationFrame on how to make this cross-browser as this is still a "young" implementation. 请参阅requestAnimationFrame的链接,了解如何制作此跨浏览器,因为这仍然是一个“年轻”实现。

dynCtx and dynCanvas can of course be called what you want. dynCtxdynCanvas当然可以被称为你想要的。

You need to erase the current contents of the canvas before redrawing your updated "grass". 在重新绘制更新的“草”之前,您需要擦除画布的当前内容。 This would involve redrawing everything else that is "static" on the canvas. 这将涉及重绘画布上“静态”的所有其他内容。 For example: 例如:

function redraw() {
  // Erase the current contents
  ctx.fillStyle = 'white';
  ctx.fill(0, 0, canvas.width, canvas.height);

  // Draw the stuff that does not change from frame to frame
  drawStaticContent();

  // Draw the dynamic content
  drawGrass();
}

In your mousemove event, update your grass curve information, then 'redraw'. mousemove事件中,更新草曲线信息,然后“重绘”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM