Solution 1 :

Just add document.getElementById("chart").appendChild(canvas);. You forgot to add canvas to document.

Problem :

I had no idea why it won’t works, same methods used in other code which works but not this one, there’s no errors and warnings at the console too, is there any wrong in my code?

Code in JSFiddle

var canvas = document.createElement("canvas");

// styling
canvas.style.position = "absolute";

canvas.height = 300;

canvas.width = 500;

canvas.style.top = "0px";
canvas.style.left = "0px";

var cxt = canvas.getContext("2d");

// title should be larger
cxt.font = "15px arial";

cxt.textAlign = "center";

cxt.fillStyle = "black";

cxt.fillText(
    "Testing Chart",
    250,
    200
);

cxt.fillRect(
    250,
    200,
    50,
    50
);

Comments

Comment posted by RexLeong

Thanks, but it doesn’t work in my other code which the canvas already appended into child of specific parent element which already the element also visible in browser and I also call

Comment posted by RexLeong

Just found the solution just now and it’s not related to the code I inserted above.

By