Topic: Canvas help (Page 1 of 1) |
|
|---|---|
|
Obsessive-Compulsive (I) Inmate From: |
posted 03-04-2010 17:34
Hello all - I'm having a bit of trouble with the html canvas, and thought I'd ask for help. I've only just started playing around with it and already I'm a bit rubbish. If anyone can offer some guidance to where I'm going wrong it would be very much appreciated. code: function world(x,y) {
this.x = x;
this.y = y;
this.create = function() {
thecanvas = document.createElement("canvas");
thecanvas.id = "worldcanvas";
thecanvas.style.width = this.x + "px";
thecanvas.style.height = this.y + "px";
thecanvas.width = this.x + "px";
thecanvas.height = this.y + "px";
window.document.body.appendChild(thecanvas);
getcanvas = document.getElementById("worldcanvas");
}
}
code: function draw(o) {
ctx = document.getElementById(o).getContext("2d");
ctx.fillRect(10,10,55,50);
}
|
|
Paranoid (IV) Inmate From: Norway |
posted 03-05-2010 09:47
The resolution of a Canvas is intrinsic: it doesn't have units. Therefore you should remove the +"px" when setting the width and height of the canvas otherwise the user agent will ignore these assignments and default to 300 x 150. See the spec: quote: source code: function World( w, h )
{
var _canvas = document.createElement('canvas'),
_context;
_canvas.width = w;
_canvas.height = h;
_context = _canvas.getContext('2d');
this.create = function()
{
_canvas.style.width = this.x + 'px';
_canvas.style.height = this.y + 'px';
window.document.body.appendChild( _canvas );
}
this.draw = function()
{
_context.fillRect( 10,10, 55,50 );
}
}
var myWorld = new World( 200,500 );
myWorld.create();
myWorld.draw(); |
|
Obsessive-Compulsive (I) Inmate From: |
posted 03-05-2010 12:50
Aha! Thank you very much sir. Definitely quite confused with OO Javascript, particularly the notion of private variables etc. What you've done should help me to figure it out a bit better. |
|
Obsessive-Compulsive (I) Inmate From: |
posted 07-30-2010 10:10
TP: Spam removed |
|
Nervous Wreck (II) Inmate From: |
posted 05-31-2011 11:03
Edit TP: spam removed
|