html canvas not filling with colour - javascript

ive been following a tutorial on how to make a simple canvas. However, the canvas isnt showing up in any colour even though i have followed the tutorial exactly. any help. its bugging me :/
<html>
some text here
<canvas id="gamecanvas" width="800" height="600></canvas>]
<script>
var Canvas;
var canvasContext;
window.onload = function() {
console.log("Hello world!");
Canvas = document.getElementById("gamecanvas");
canvasContext = canvas.getcontext('2d');
canvasContext.fillStyle = 'red';
canvasContext.fillRect(0,0,canvas.width,canvas.height);
}
</script>
</html>
edit: Thanks for the help. Solved!

Too many syntax errors.
This is working
<html>
some text here
<canvas id="gamecanvas" width="800" height="600"></canvas>
<script>
var Canvas;
var canvasContext;
window.onload = function() {
console.log("Hello world!");
Canvas = document.getElementById("gamecanvas");
canvasContext = Canvas.getContext('2d');
canvasContext.fillStyle = 'red';
canvasContext.fillRect(0,0,Canvas.width,Canvas.height);
}
</script>
</html>

You have done two mistakes
You were mixing canavs and Canvas
You have wrote getContext with a none-capital c
Dont forget that variables and method-names are casesensitve in JavaScript.
So canavas isnt the same variable as Canavs and getcontext('2d') isn't the same method as getContext('2d').
Heres a Fiddle in which I have corrected the errors:
var canvas;
var canvasContext;
window.onload = function() {
console.log("Hello world!");
canvas = document.getElementById("gamecanvas");
canvasContext = canvas.getContext('2d');
canvasContext.fillStyle = 'red';
canvasContext.fillRect(0,0,canvas.width,canvas.height);
}
some text here
<canvas id="gamecanvas" width="800" height="600"></canvas>

Related

Circle not appearing over image in html 5

I am trying to add a circle over the image and I am using the .onload function too but the circle is still being drawn below the image.
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="1024" height="500" style="border:1px solid #d3d3d3;">
Your browser does not support the canvas element.
</canvas>
<script>
var canvas = document.getElementById("myCanvas");
var background = new Image();
background.src = "https://i.imgur.com/ua7gL3M.png";
// Make sure the image is loaded first otherwise nothing will draw.
background.onload = function(){
ctx.drawImage(background,0,0);
}
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(512,200,60,0,2*Math.PI);
ctx.strokeStyle = "red"
ctx.lineWidth = 5;
ctx.stroke();
</script>
</body>
</html>
When I run the code snippet, there's a brief moment the circle is visible before the image is rendered. The image has to wait to load before it is rendered, but the circle is being drawn immediately. Because of that, the circle is drawn first then the image is placed on top of it. To fix this, you can draw the circle after the image is rendered. See this revised code snippet:
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="1024" height="500" style="border:1px solid #d3d3d3;">
Your browser does not support the canvas element.
</canvas>
<script>
var canvas = document.getElementById("myCanvas");
var background = new Image();
background.src = "https://i.imgur.com/ua7gL3M.png";
// Make sure the image is loaded first otherwise nothing will draw.
background.onload = function(){
ctx.drawImage(background,0,0);
// The following lines were moved into the onload callback
ctx.beginPath();
ctx.arc(512,200,60,0,2*Math.PI);
ctx.strokeStyle = "red"
ctx.lineWidth = 5;
ctx.stroke();
}
var ctx = canvas.getContext("2d");
</script>
</body>
</html>

my image wont load..... "image not found" any ideas why?

I am trying to load an image of me on my html canvas.....
The image is in the right folder...... I just get a weird error (as shown above) im not sure what is going on if you know please let me know.
Here is the code:
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="800" height="500"></canvas>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "black";
ctx.fillRect(0,0,800,500)
var img = new Image();
img.src = "science.PNG";
img.onload = function() {
context.drawImage(img, 145, 145);
};
</script>
</body>
</html>
context.drawImage(img, 145, 145);
Try changing 'context' to 'ctx'.

Javascript: How do I zoom in my Canvas so that my functions are still working?

Dear StackOverflow community,
I'm currently working on a project. My goal is to color individual pixels in a zoomed in canvas.
However when I used:
canvas.body.style.width = '600px';
canvas.body.style.height= '300px';
my functions stopped working.
Here is my JavaScript code:
<!DOCTYPE html>
<html>
<meta charset='UTF-8'>
<body>
<canvas id="myCanvas" width="200" height="200" style="border:3px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var mouseDown = 0;
function mouseDownFunction() {
++mouseDown;
}
function mouseUpFunction() {
--mouseDown;
}
function mouseMoveFunction(e) {
var posX = e.clientX;
var posY = e.clientY;
ctx.fillRect(posX - 10, posY - 10, 100, 100);
}
c.addEventListener("mousedown", function(e){
mouseDownFunction();
this.addEventListener("mousemove", mouseMoveFunction);
});
c.addEventListener("mouseup", function(e){
mouseUpFunction();
this.removeEventListener("mousemove", mouseMoveFunction);
});
c.addEventListener("click", mouseMoveFunction);
c.addEventListener("mouseout",function(e){
this.removeEventListener("mousemove", mouseMoveFunction);
});
c.addEventListener("mouseenter",function(e){
if (mouseDown == 1) {
this.addEventListener("mousemove", mouseMoveFunction);
}});
</script>
</body>
</html>
What's the best practice to zoom in and out of my canvas to allow easy access to individual pixels?
Thanks in advance for your time.
Sincerely,
JavaIsMyBae

Draw image with button

hi i am trying to draw image with a button in canvas. But i can not draw in the clear canvas. so;
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Başlıksız Belge</title>
</head>
<body>
<input type="button" id="b1" value="ciz">
<canvas id="canvas" height="300" width="600" style="border: solid;background-color: brown; " >Eski Sürüm Tarayıcı . </canvas>
<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var b1 =document.getElementById("b1");
var image = new Image(60, 45); // using optional size for image
var kontrol;
image.src = '/pika.png'; //it is in my pc
image.onload = function(){ // i don't want draw the image on load
kontrol=true;}
b1.onclick=function(){ // i want draw with button
if(kontrol=true){
ctx.drawImage(image, 0, 0);
}
}
</script>
</body>
</html>
when i try with image on load function it is working but i want draw with the button
No problem i get it. thanks...
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var b1 =document.getElementById("b1");
var image = new Image(60, 45);
b1.onclick=function(){
image.src = 'https://i.hizliresim.com/nWdGrR.png';
image.onload = function(){
ctx.drawImage(image, 0, 0);
}
}
If pika.png is located in same directory as your html file then just remove the forward slash before your image's name: image.src = 'pika.png';

drawImage (canvas) is not working

I want to draw multiple canvases on a single canvas using drawImage() method but it is not working
Code
<html>
<head>
<script>
window.onload = function() {
var context1= document.getElementById("canvas1").getContext("2d");
var context2 = document.getElementById("canvas2").getContext("2d");
var context3 = document.getElementById("canvas3").getContext("2d");
context1.font = "20pt Calibri";
context1.fillStyle = "blue";
context1.fillText("Hello ", 50, 25);
context2.font = "20pt Calibri";
context2.fillStyle = "red";
context2.fillText("World!", 100, 25);
var imageObj = new Image();
imageObj.onload = function() {
context3.drawImage(context1.canvas, 50, 25);
context3.drawImage(context2.canvas, 100, 25);
};
};
</script>
</head>
<body>
<canvas id="canvas1" class="canvas" width="200" height="50"></canvas>
<canvas id="canvas2" class="canvas" width="200" height="50"></canvas>
<canvas id="canvas3" class="canvas" width="200" height="50"></canvas>
</body>
</html>​
JS Fiddle
http://jsfiddle.net/4xT2j/2/
Your images have no source. Add one if you want to see something.
The onload function cannot be called as long as you don't have a src.
And you must provide the image to the drawImage function :
var imageObj = new Image();
imageObj.onload = function() {
context3.drawImage(imageObj, 50, 25);
context3.drawImage(imageObj, 100, 25);
};
imageObj.src = "someFile.png";
If what you're trying to do is draw canvas1 and canva2 on context3, simply do all this outside the imageObj.onload function which is never called : http://jsfiddle.net/KCyvE/
A precision following a question in comment :
My code in the fiddle uses context1.canvas. This works because The context is an instance of CanvasRenderingContext2D and thus has a property named canvas which is a "Back-reference to the canvas element for which this context was created".
Your imageObj.onload function is somewhat wrong. This is what you want: http://jsfiddle.net/4xT2j/3/
Please observe there isn't necessarily a reason to write canvas3 to an image object since it already is an image object.

Categories

Resources