🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to JavaScript Notes
Topic #250

JS Canvas

HTML Canvas is perfect for Scatter Plots

HTML Canvas is perfect for Line Graphs

HTML Canvas is perfect for combining Scatter and Lines

Scatter Plots

Source Code

const xArray = [50,60,70,80,90,100,110,120,130,140,150];

const yArray = [7,8,8,9,9,9,10,11,14,14,15];

// Plot Scatter

ctx.fillStyle = "red";

for (let i = 0; i < xArray.length-1; i++) {

  let x = xArray[i]*400/150;

  let y = yArray[i]*400/15;

  ctx.beginPath();

  ctx.ellipse(x, y, 2, 3, 0, 0, Math.PI * 2);

  ctx.fill();

}

Line Graphs

Source Code

let xMax = canvas.height;

let slope = 1.2;

let intercept = 70;

// Plot Scatter

ctx.moveTo(0, intercept);

ctx.lineTo(xMax, f(xMax));

ctx.strokeStyle = "black";

ctx.stroke();

// Line Function

function f(x) {

  return x * slope + intercept;

}

Combined

Source Code

let xMax = canvas.height;

let yMax = canvas.width;

let slope = 1.2;

let intercept = 70;

const xArray = [50,60,70,80,90,100,110,120,130,140,150];

const yArray = [7,8,8,9,9,9,10,11,14,14,15];

// Plot Scatter

ctx.fillStyle = "red";

for (let i = 0; i < xArray.length-1; i++) {

  let x = xArray[i]*400/150;

  let y = yArray[i]*400/15;

  ctx.beginPath();

  ctx.ellipse(x, y, 2, 3, 0, 0, Math.PI * 2);

  ctx.fill();

}

// Plot Line

ctx.moveTo(0, intercept);

ctx.lineTo(xMax, f(xMax));

ctx.strokeStyle = "black";

ctx.stroke();

// Line Function

function f(x) {

  return x * slope + intercept;

}

Want to go beyond the notes?

Join CodingNow 2.0's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

JS Canvas – FAQs

Quick answers about learning JS Canvas in JavaScript.

This free note from CodingNow 2.0 explains JS Canvas in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on CodingNow 2.0, including JS Canvas, is 100% free with no signup required.
With focused practice, most students grasp JS Canvas in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now