简体   繁体   English

画布特殊形状-动画

[英]Canvas special shape - animating

I'm finishing a project, but I have one more step to finish. 我正在完成一个项目,但还有一个步骤要完成。 I want to visualize microphone input by a canvas. 我想通过画布可视化麦克风输入。 Getting the data from the microphone isn't a problem. 从麦克风获取数据不是问题。 But I want to visualize it in a special way. 但是我想以一种特殊的方式来可视化它。 (see image) (见图片)

波

I want to animate each element from the wave. 我想为wave中的每个元素设置动画。

My problem isn't the animation. 我的问题不是动画。 My problem is to create those shapes in the CANVAS. 我的问题是在CANVAS中创建这些形状。 This is an example of one shape: 这是一个形状的示例:

一种形状

I can create a rounded corner shape with the canvas 我可以用画布创建圆角形状

    const draw = () => {
        fillRoundedRect(20, 20, 100, 100, 20);
        ctx.fillStyle = "red";
        ctx.fill();
    };

    const fillRoundedRect = (x, y, w, h, r) => {
        ctx.beginPath();
        ctx.moveTo(x+r, y);
        ctx.lineTo(x+w-r, y);
        ctx.quadraticCurveTo(x+w, y, x+w, y+r);
        ctx.lineTo(x+w, y+h-r);
        ctx.quadraticCurveTo(x+w, y+h, x+w-r, y+h);
        ctx.lineTo(x+r, y+h);
        ctx.quadraticCurveTo(x, y+h, x, y+h-r);
        ctx.lineTo(x, y+r);
        ctx.quadraticCurveTo(x, y, x+r, y);
        ctx.fill();
    };

Can someone help me with creating a shape like in the second image? 有人可以帮助我创建第二张图片中的形状吗?

Thanks in advance guys! 在此先感谢大家!

Instead of trying to make a single shape with dependency on surrounding shapes and a high risk of headache math-wise, use instead two shapes which you merge using composition. 与其尝试制作一个依赖于周围形状的单一形状,并避免数学上出现头痛的高风险,不如使用两个形状,然后使用合成将它们合并。 My suggestion anyways. 反正我的建议。

  • Draw all the bars in full height using composition mode source-over (default) 使用合成模式source-over (默认)以全高绘制所有条形图
  • Define a single shape on top using some sort of spline (I would suggest a cardinal spline ). 使用某种样条线在顶部定义单个形状(我建议使用基数样条线 )。
  • Set composition mode to destination-out and render an enclosed shape using the spline as top "line". 将合成模式设置为destination-out并使用样条线作为顶部“线”渲染封闭的形状。

Example

This should work in a loop (remember to clear canvas for each frame) but shows only the building stones needed here - 这应该可以循环工作(请记住为每个框架清除画布),但此处仅显示所需的建筑石材-

var ctx = c.getContext("2d");
var points = [];
var skippy = 0;

// render all bars
ctx.globalCompositeOperation = "source-over"; // not needed here, but in a loop yes!

// produce bars
ctx.beginPath();                             // not needed here, but in a loop yes!
for(var x = 0; x < c.width; x += 30) {
  ctx.rect(x, 0, 16, c.height)

  // OKIDOKI, lets produce the spline using random points (y) as well
  // but not for all, only every second for prettyness... modify to taste
  if (skippy++ % 2 === 0) points.push(x, c.height * Math.random());
}
points.push(c.width, c.height * Math.random());  // one last
ctx.fillStyle = "rgb(198, 198, 198)";
ctx.fill();

// render spline
ctx.beginPath();
ctx.moveTo(0, c.height);                     // bottom left corner
curve(ctx, points);                          // spline
ctx.lineTo(c.width, c.height);               // bottom right corner
ctx.closePath();
ctx.globalCompositeOperation = "destination-out";
ctx.fill();

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

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