Published
Edited
Aug 5, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const ctx = DOM.context2d();
ctx.canvas.width = width;
ctx.canvas.height = height;

// Draw the control points.
ctx.strokeStyle = 'coral';
ctx.beginPath();
ctx.arc(cpx1, cpyAnim1, 5, 0, 2 * Math.PI);
ctx.stroke();
ctx.beginPath();
ctx.arc(cpx2, cpyAnim2, 5, 0, 2 * Math.PI);
ctx.stroke();

// The clip path.
ctx.beginPath();
ctx.moveTo(0, height);
ctx.lineTo(0, y);
ctx.quadraticCurveTo(cpx1, cpyAnim1, endX1, y);
ctx.quadraticCurveTo(cpx2, cpyAnim2, endX2, y);
ctx.lineTo(width, height);
ctx.closePath();
ctx.clip();

// Then, draw the clip content
ctx.fillStyle = 'skyblue';
ctx.fillRect(0, 0, width, height);

// Draw control points again (to show in front of blue wave).
ctx.strokeStyle = 'coral';
ctx.beginPath();
ctx.arc(cpx1, cpyAnim1, 5, 0, 2 * Math.PI);
ctx.stroke();
ctx.beginPath();
ctx.arc(cpx2, cpyAnim2, 5, 0, 2 * Math.PI);
ctx.stroke();

return ctx.canvas;
}
Insert cell
cpyAnim2 = y - amplitude * weight
Insert cell
cpyAnim1 = y + amplitude * weight
Insert cell
Insert cell
weight = {
const weight = {
top: lift,
bottom: 1 - lift,
both: 1 - lift < 0.5 ? 1 - lift : lift,
none: 1
};
return weight[weightType];
}
Insert cell
amplitude = Math.sin(now / 1000) * 150
Insert cell
y = height * lift
Insert cell
lift = (Math.sin(now / 2000) + 1) / 2
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
yOffset = Math.sin(now / 1000) * 0.3
Insert cell
cpy1 = height * 0.5 + height * yOffset
Insert cell
Insert cell
Insert cell
Insert cell
xOffset = xMove === "true" ? Math.cos(now / 1000) * 0.25 : 0
Insert cell
cpx1 = width * 0.25 + width * xOffset
Insert cell
endX1 = width * 0.5 + width * xOffset
Insert cell
cpx2 = width * 0.75 + width * xOffset
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more