I am new to this and have not actually done any JavaScript before, so I hope you can help me. I made a canvas that allows the user to select a shape and color using the switches that are then drawn on the canvas. I also added a flag with the ability to add a gradient to the selected color. Here's the program: http://people.dsv.su.se/~caak1743/Canvas/canvas.html
Now I will not make it so that the figures can be dragged and dropped around the canvas. And I found code that I think can be modified to work on my program, but I keep getting: TypeError: It is not possible to call the getContext null method on this (anonymous function) for the string:
var ctx = canvas.getContext("2d");
and I donβt know what is wrong, or how I can solve it. I tried looking for similar programs with similar problems, but did not find anything that I can apply here. Here is the code I'm trying to include with mine:
function init() { var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); return setInterval(draw, 10); } function draw() { clear(); ctx.fillStyle = "#FAF7F8"; rectangle(0,0,WIDTH,HEIGHT); ctx.fillStyle = "#444444"; rectangle(); } function myMove(e){ if (dragok){ x = e.pageX - canvas.offsetLeft; y = e.pageY - canvas.offsetTop; } } function myDown(e){ if (e.pageX < x + 15 + canvas.offsetLeft && e.pageX > x - 15 + canvas.offsetLeft && e.pageY < y + 15 + canvas.offsetTop && e.pageY > y -15 + canvas.offsetTop){ x = e.pageX - canvas.offsetLeft; y = e.pageY - canvas.offsetTop; dragok = true; canvas.onmousemove = myMove; } } function myUp(){ dragok = false; canvas.onmousemove = null; } init(); canvas.onmousedown = myDown; canvas.onmouseup = myUp;
source share