Canvas element มี DOM method ชื่อ getContext ใช้เพื่อได้ rendering context และ drawing functions ฟังก์ชันนี้รับพารามิเตอร์หนึ่งตัว คือประเภท context '2d'
โค้ดด้านล่างเพื่อได้ context ที่ต้องการ พร้อมตรวจสอบว่าเบราว์เซอร์รองรับ <canvas> element:
var canvas = document.getElementById("mycanvas");
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
// drawing code here
} else {
// canvas-unsupported code here
}