plot a heart in javascript

To plot a heart in JavaScript, you can use the HTML canvas element and draw two half-circles and two Bézier curves to create the shape. Here is an example code snippet:

<canvas id="canvas" width="200" height="200"></canvas>
55 chars
2 lines
index.tsx
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const size = 60;

ctx.beginPath();
ctx.moveTo(centerX, centerY - size / 4);
ctx.bezierCurveTo(centerX + size / 2, centerY - size / 3, centerX + size / 2, centerY + size / 2, centerX, centerY + size / 2);
ctx.bezierCurveTo(centerX - size / 2, centerY + size / 2, centerX - size / 2, centerY - size / 3, centerX, centerY - size / 4);
ctx.closePath();
ctx.fillStyle = 'red';
ctx.fill();
541 chars
15 lines

This code draws a red heart shape on a 200x200 canvas using the Bézier curves. You can adjust the size variable to change the size of the heart.

gistlibby LogSnag