JSFiddle - React, Tailwind, and code Playground
by pedro_g_s
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Canvas Gradient Line</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
canvas {
border: 1px solid #000;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="500" height="500"></canvas>
<script>
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
// Create gradient
const gradient = ctx.createLinearGradient(50, 250, 450, 250);
gradient.addColorStop(0, 'red');
gradient.addColorStop(1, 'black');
// Set the gradient as stroke style
ctx.strokeStyle = gradient;
ctx.lineWidth = 5;
// Draw the line
ctx.beginPath();
ctx.moveTo(50, 250);
ctx.lineTo(450, 250);
ctx.stroke();
</script>
</body>
</html>