1 decade ago by alexandre
I'm running into some problem I don't quite understand. I borrowed the DropLoader code from Drop to pixify the project's images. Code for main is below.
Java console reports:
for this line:
Full source for main class follows:
Java console reports:
Uncaught TypeError: Object #<HTMLImageElement> has no method 'getContext'
for this line:
pixify: function (img, s) {
var ctx = img.data.getContext ('2d');
Full source for main class follows:
ig.module(
'game.main'
)
.requires(
'impact.game',
'game.fullsize-backdrop',
'game.title'
)
.defines(function()
{
// from Impact Drop.js: a custom loader that, after all
// images have been loaded, goes through and "pixifies"
// them (aka LCD effect)
MyLoader = ig.Loader.extend (
{
end: function ()
{
for (i in ig.Image.cache)
{
var img = ig.Image.cache[i];
if (! (img instanceof FullsizeBackdrop))
{
this.pixify (img, ig.system.scale);
}
}
this.parent ();
},
// function to delete last row and column of pixels for each
// upscaled pixel
pixify: function (img, s)
{
var ctx = img.data.getContext ('2d'); // <= ERROR
var px = ctx.getImageData (0, 0, img.data.width, img.data.height);
for( var y = 0; y < img.data.height; y++ )
{
for( var x = 0; x < img.data.width; x++ )
{
var index = (y * img.data.width + x) * 4;
var alpha = (x % s == 0 || y % s == 0) ? 0 : 0.9;
px.data[index + 3] = px.data[index + 3] * alpha;
}
}
ctx.putImageData (px, 0, 0);
}
});
// Start the Game w. Title (a Game subclass) with 60fps,
// a resolution of 384x480, scale up by a factor of 1
ig.main ('#canvas', Title, 60, 320, 480, 1, MyLoader);
});
