Skip to content

Commit

Permalink
feat(practice): love
Browse files Browse the repository at this point in the history
  • Loading branch information
xjq7 committed Nov 6, 2024
1 parent 046cbb4 commit 6a901d1
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/pages/practice/draw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,69 @@ function brick(ctx: CanvasRenderingContext2D) {
}
}

function test(ctx: CanvasRenderingContext2D) {
const gradient = ctx.createRadialGradient(200, 300, 10, 200, 0, 100);

gradient.addColorStop(0, 'blue');
gradient.addColorStop(0.25, 'white');
gradient.addColorStop(0.5, 'purple');
gradient.addColorStop(0.75, 'red');
gradient.addColorStop(1, 'yellow');

ctx.fillStyle = gradient;
ctx.rect(0, 0, 400, 300);
ctx.fill();
}

function love(ctx: CanvasRenderingContext2D) {
const drawLeft = () => {
ctx.beginPath();

const p0 = [200, 200];
const p1 = [110, 170];
const p2 = [143, 260];
const p3 = [200, 300];
ctx.moveTo(p0[0], p0[1]);
ctx.bezierCurveTo(p1[0], p1[1], p2[0], p2[1], p3[0], p3[1]);
ctx.fillStyle = 'red';
ctx.fill();

ctx.beginPath();
ctx.moveTo(p0[0], p0[1]);
ctx.lineWidth = 1;
[p1, p2, p3].forEach((p) => {
ctx.lineTo(p[0], p[1]);
});
ctx.strokeStyle = 'blue';
ctx.stroke();
};

const drawRight = () => {
ctx.beginPath();
const p0 = [200, 200];
const p1 = [290, 165];
const p2 = [257, 260];
const p3 = [200, 300];
ctx.moveTo(p0[0], p0[1]);
ctx.bezierCurveTo(p1[0], p1[1], p2[0], p2[1], p3[0], p3[1]);
ctx.fillStyle = 'red';
ctx.fill();

ctx.beginPath();
ctx.moveTo(p0[0], p0[1]);
ctx.lineWidth = 1;
[p1, p2, p3].forEach((p) => {
ctx.lineTo(p[0], p[1]);
});
ctx.strokeStyle = 'blue';
ctx.stroke();
};
drawLeft();
drawRight();
}

export const drawFns = {
love,
brick,
'pie-chart': pieChart,
histogram,
Expand All @@ -301,4 +363,5 @@ export const drawFns = {
arc,
rect,
ticTac,
test,
};

0 comments on commit 6a901d1

Please sign in to comment.