html5 drawing multiple canvas images - html

i've got a problem with canvas in html. I want to draw multiple canvas images.
I have created a loop which creates multiple canvas and then i want to draw in each canvas an image.
for (var i = 1; i <= playerStuff.MovingCards.length; i++){
$("#playersField").append("<canvas id='movingCardsField"+i+"' height='"+movingCardSizeY+"'\n\
width='"+movingCardSizeX+"'\n\
data='"+playerStuff.MovingCards[i-1].movePoints+"'></canvas>");
var imgObj = new Image();
var drawField = document.getElementById('movingCardsField'+i).getContext("2d");
imgObj.onload = function(){
drawField.drawImage(imgObj, 0,0);
};
imgObj.src = "./img/moveCards/"+playerStuff.MovingCards[i-1].name;
}
The result is that only the last canvas has a drawn image. What am I missing here?

Please dont use JQuery for this. In generally i'm not a friend of JQuery but thats my personally opinion,but here you should really dont useit, use the DOM its much more faster.
var playersField=document.getElementById("playersField");
var canvasElements=new Array ();
var canvasElement, drawField,imgObj;
var drawFields=new Array ();
for (var i = 0; i <= playerStuff.MovingCards.length; i++)
{
canvasElement=document.createElement ("canvas");
canvasElement.height=movingCardSizeY;
canvasElement.width=movingCardSizeX;
canvasElement.data=playerStuff.MovingCards[i-1].movePoints;
playersField.appendChild(canvasElement);
drawField=canvasElement.getContext("2d");
drawFields.push (drawField);
imgObj = new Image();
imgObj.src = "./img/moveCards/"+playerStuff.MovingCards[i-1].name;
imgObj.index=i;
imgObj.onload = function (){
drawFields[this.index].drawImage(this, 0,0);
};

that is becouse onload y asinchronous, when al the iterations of the for has ended, the onload is probably that has not been called. then drawField variable is the last element. to solve this in a fast way (but not the cleanest solution)
the scope of a varaible is not as it could be in c++ or other languages.
for (var i = 1; i <= playerStuff.MovingCards.length; i++){
(function(element) {
$("#playersField").append("<canvas id='movingCardsField"+i+"' height='"+movingCardSizeY+"'\n\
width='"+movingCardSizeX+"'\n\
data='"+element.movePoints+"'></canvas>");
var imgObj = new Image();
var drawField = document.getElementById('movingCardsField'+i).getContext("2d");
imgObj.onload = function(){
drawField.drawImage(imgObj, 0,0);
};
imgObj.src = "./img/moveCards/"+element.name;
})(playerStuff.MovingCards[i - 1]);
}
this may do the trick, more or less, I didn't check twice the code
btw is not very efficient also
this should be something better
var domElement = $("#playersField");
var canvasStart = "<canvas id='movingCardsField";
var canvasEnd = "' height='"+movingCardSizeY + "'width='" + movingCardSizeX + "' data='" + element.movePoints + "'></canvas>";
function createCanvas(element) {
var canvas = canvasStart + (i + 1) + canvasEnd;
domElement.append(canvas);
var imgObj = new Image();
var drawField = document.getElementById('movingCardsField' + (i + 1)).getContext("2d");
imgObj.onload = function() {
drawField.drawImage(imgObj, 0,0);
};
imgObj.src = "./img/moveCards/" + element.name;
}
for (var i = 0; i < playerStuff.MovingCards.length; i++) {
createCanvas(playerStuff.MovingCards[i]);
}

Related

Multiple images in html5 canvas not stacking in the right sequence

I'm adding multiple png transparent images to an html5 canvas one above the other but they don't stack in the right sequence (defined by the array containing the url of these images)
I read in other discussions that it may be a closure problem but no solution as far.
This is my code:
function drawCanvas() {
var canvas;
var context;
var imgArray = [
'images/img1.png',
'images/img2.png',
'images/img3.png',
'images/img4.png',
'images/img5.png',
'images/img6.png'
];
canvas = document.getElementById('myCanvas');
context = canvas.getContext("2d");
for (var i = 0; i < imgArray.length; i++) {
var img = new Image();
img.src = imgArray[i];
var drawTee = function(i, context, canvas) {
return function() {
console.log(i, 'loaded')
context.drawImage(this, 0, 0);
}
};
img.onload = drawTee(i, context, canvas);
}
};
Can anyone help me?
The problem is that the order of images on the canvas is determined by the order in which the images finish loading.
Solution: Wait for all the images to finish loading and then add them to the canvas
function drawCanvas() {
var canvas;
var context;
var imgArray = [
'images/img1.png',
'images/img2.png',
'images/img3.png',
'images/img4.png',
'images/img5.png',
'images/img6.png'
];
var images = [];
canvas = document.getElementById('myCanvas');
context = canvas.getContext("2d");
var loadCount = 0;
for (var i = 0; i < imgArray.length; i++) {
var img = new Image();
img.src = imgArray[i];
images.push(img);
img.onload = function() {
if(++loadCount == imgArray.length) {
for(var i=0; i < imgArray.length; i++) {
context.drawImage(images[i], 0, 0);
}
}
};
}
};

duplicateMovieClip in Action Script 3

My question is how to do this:
https://www.dropbox.com/s/zi63y771h38a2vo/Example.fla
In Action Script 3.0.
This is working timeline source code, with ready indexing values + sorting news via array.
Simple as that, how to create same thing in ActionScript 3.0?
I was searching on the websites/forums and I couldn't find any answer that was satisfying me, so I decided to create an account in here and ask for help.
If someone could remake this example on ActionScript 3.0, this could help us all, because I saw a lot of questions about duplicateMovieClip() function, but there were no strict answer + example on it, so maybe let's create this?
This is my suggestion, code is in file or here:
stop();
var IDMovieClip = 0;
var IDarray = 0;
var Duplicate:MovieClip;
MC._visible = false;
var ARRAY:Array = new Array();
ENTER.onRelease = function() {
Duplicate = MC.duplicateMovieClip(IDMovieClip, _root.getNextHighestDepth());
var ref = eval(Duplicate);
ref.ID = IDMovieClip;
ref.sortedID = IDarray;
_root[ref.ID].windowID.text = "ID: " + ref.ID;
Duplicate.Close.onRollOver = function() {
trace(_root[ref.ID]._target);
};
Duplicate.Close.onRelease = function() {
_root.ARRAY.splice(_root[ref.ID].sortedID,1);
removeMovieClip(_root[ref.ID]);
IDarray -= 1;
_root.doSort();
};
ARRAY.push([IDarray, IDMovieClip]);
doSort();
IDMovieClip += 1;
IDarray += 1;
};
doSort = function () {
for (var i = 0; i < ARRAY.length; i++) {
_root[ARRAY[i][1]]._y = 10 + ((_root[ARRAY[i][1]]._height + 10) * i);
_root[ARRAY[i][1]].sortID.text = i;
_root[ARRAY[i][1]].sortedID = i;
trace(ARRAY[i]);
}
};
FLA PROJECT DESIGN IN JPG (MovieClips/Placement etc)
(what You need to run it, if You dont want to download it from my DropBox)
If anyone could help, that would be great.
There is no duplicateMovieClip in AS3, in ActionScript 3, you instantiate movie clips. I didn't find place for all used variables, I think it's part of some project, so you should adapt code for your needs. You also should read a bit about Export for ActionScript.
//Container for your objects
var movieHolder: Sprite = new Sprite();
var id:uint = 0;
const padding: int = 10;
//Handler, that will add new objects to the scene
enter.addEventListener(MouseEvent.CLICK, onClickEnter);
addChild(movieHolder);
movieHolder.x = movieHolder.y = padding;
function onClickClose(e:MouseEvent):void {
movieHolder.removeChild(DisplayObject(e.currentTarget).parent);
sortMovies();
}
function onClickEnter(e:MouseEvent):void {
//Set up for MovieClip with form export for ActionScript
var movie:MyFormMovie = new MyFormMovie();
movie.windowID.text = "ID: " + id;
movie.Close.addEventListener(MouseEvent.CLICK, onClickClose, false, 0, true);
movieHolder.addChild(movie);
sortMovies();
id++;
}
function sortMovies():void {
var i: uint, len: uint = movieHolder.numChildren, movie: MyFormMovie;
var posY: uint;
for(i; i < len; ++i){
movie = movieHolder.getChildAt(i) as MyFormMovie;
movie.y = posY;
movie.sortID.text = i.toString();
posY += movie.height + padding;
}
}

How to convert this google maps v2 API function to v3?

I need to convert this function to v3 API. This function is used to create an ellipse under a marker when selected. I have tried with projection and overlay, but without success.
function makePolyPoints(_marker)
{
var polyPoints = Array();
var markerPoint= _marker.getLatLng();
var projection = G_NORMAL_MAP.getProjection();
var mapZoom = map.getZoom();
var clickedPixel = projection.fromLatLngToPixel(markerPoint, mapZoom);
var ellipseRadA = 20;
var ellipseRadB = 10;
var polyNumSides = 20;
var polySideLength = 18;
for (var a = 0; a<(polyNumSides+1); a++) {
var aRad = polySideLength*a*(Math.PI/180);
var pixelX = clickedPixel.x + ellipseRadA * Math.cos(aRad);
var pixelY = -3 + clickedPixel.y + ellipseRadB * Math.sin(aRad);
var polyPixel = new GPoint(pixelX,pixelY);
var polyPoint = projection.fromPixelToLatLng(polyPixel,mapZoom);
polyPoints.push(polyPoint);
}
return polyPoints;
}
Here the function for v3 that doesn't work, i think a problem of zoom level but I can't find how to replace projection.fromLatLngToPixel(markerPoint, mapZoom);
function makePolyPoints(_marker)
{
var polyPoints = Array();
var markerPoint= _marker.getPosition();
var projection = map.getProjection();
var mapZoom = map.getZoom();
var clickedPixel = projection.fromLatLngToPoint(markerPoint);
var ellipseRadA = 20;
var ellipseRadB = 10;
var polyNumSides = 20;
var polySideLength = 18;
for (var a = 0; a<(polyNumSides+1); a++) {
var aRad = polySideLength*a*(Math.PI/180);
var pixelX = clickedPixel.x + ellipseRadA * Math.cos(aRad);
var pixelY = -3 + clickedPixel.y + ellipseRadB * Math.sin(aRad);
var polyPixel = new google.maps.Point(pixelX,pixelY);
var polyPoint = projection.fromPointToLatLng(polyPixel);
polyPoints.push(polyPoint);
}
return polyPoints;
}
Hmm.. Im going to use one of my favorite gmap examples to help you out.
Using var clickedPixel = projection.fromLatLngToPoint(markerPoint); dont give you yet clickedPixel coordinates, they are still world coordinates. See projection specs.
Specifying own mercator projection when calculating that kind of stuff is useful. Look closely to following lines: (in this code example)
var worldCoordinate = projection.fromLatLngToPoint(chicago);
var pixelCoordinate = new google.maps.Point(
worldCoordinate.x * numTiles,
worldCoordinate.y * numTiles);
(you can view source with firebug or similar).
If you dont have it (in mozilla: Tools -> web developer -> page source)
Hopefully, this will help you get through the problem :)

AS3: can't addchild indexed movieclips into a sprite

I'm creating a dynamic blocked terrain in flash (AS3), and everything goes fine with it, the terrain is correctly placed. But I need to include collisions and I want the blocks to be within a movieclip (sprite), so I can test the collision with the terrain itself.
Ps: I don't know if it would be good to test the collisions with each block individually because I'll use a enterframe function and the block generation is dynamic.
The problem I'm facing is that I have a sprite called blockHolder, but I can't addChild the blocks to it.
Here's the code (I simplified it so we have the blocks being created in cascade if you addChild them into the stage directly, like addChild(clonedSquare).
The error I'm receiving:
TypeError: Error #1009: Can't access property or method of a null object reference.
var blockHolder:Sprite = new Sprite();
var clonedSquare = new square();
var lowestPoint:int = 10;
var highestPoint:int = 20;
var areaLenght:int = 10;
function createLvl():void
{
for (var i:Number = 0; i<(areaLenght); i++)
{
clonedSquare = new square();
clonedSquare.x = i * clonedSquare.width;
//sets the height of the first block
if (i == 0)
{
var firstY:Number = Math.ceil(Math.random()*((lowestPoint-highestPoint))+highestPoint)*clonedSquare.height;
clonedSquare.y = firstY;
trace("terrain begins " + firstY + " px down");
}
else
{
var previousId:Number = i - 1;
clonedSquare.y = getChildByName("newSquare"+previousId).y + clonedSquare.height;
}
//sets the entity (block) name based on the iteration
clonedSquare.name = "newSquare" + i;
//adds the cloned square
blockHolder.addChild(clonedSquare);
}
addChild(blockHolder);
}
createLvl();
Well I fixed the error. I am still not clear as to what you're asking for. Basically I add each block to an array and reference the block that way. Your clonedSquare.y = getChildByName("newSquare"+previousId).y + clonedSquare.height; was throwing the error. Also your firstY was placing the first block way off my stage so I just set it to 0 as firstY
var blockHolder:Sprite = new Sprite();
var squares:Array = [];
var lowestPoint:int = 10;
var highestPoint:int = 20;
var areaLenght:int = 10;
function createLvl():void
{
for (var i:Number = 0; i<(areaLenght); i++)
{
var clonedSquare = new square();
clonedSquare.x = i * clonedSquare.width;
if (i == 0)
{
var firstY:Number = Math.ceil(Math.random()*((lowestPoint-highestPoint))+highestPoint)*clonedSquare.height;
//clonedSquare.y = firstY;
clonedSquare.y = 0;
trace("terrain begins " + firstY + " px down");
}
else
{
clonedSquare.y = squares[i - 1].y + clonedSquare.height;
}
blockHolder.addChild(clonedSquare);
squares.push(clonedSquare);
}
addChild(blockHolder);
}
createLvl();

AS3 proportionally scaling external image

Currently I am using a for loop to dynamically load XML images and place them in a grid as thumbnails. I have the arrangement set and all the data is loading smoothly, but now I need to make the images scale to small 100px x 100px thumbs in small container movieclips. My code is as follows.
import gs.*;
import gs.easing.*;
var bttnHeight:Number = 20;
var select:Number = 0;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, showXML);
xmlLoader.load(new URLRequest("testxml.xml"));
var list_mc:Array = new Array();
function showXML(e:Event):void {
XML.ignoreWhitespace = true;
var nodes:XML = new XML(e.target.data);
var gallcount = nodes.gallery.length();
var list_mc = new listitem();
//Generate menu to select gallery
function populateMenu():void {
var spacing:Number = 0;
for (var i=0; i<gallcount; i++) {
list_mc[i] = new listitem();
list_mc[i].name = "li" + i;
list_mc[i].y = i*bttnHeight;
list_mc[i].gallname.text = nodes.gallery[i].attributes();
menu_mc.addChild(list_mc[i]);
list_mc[i].addEventListener(MouseEvent.ROLL_OVER, rollover);
list_mc[i].addEventListener(MouseEvent.ROLL_OUT, rollout);
list_mc[i].buttonMode = true;
list_mc[i].mouseChildren = false;
}
menu_mc.mask = mask_mc;
}
//list_mc.mask(mask_mc);
var boundryWidth = mask_mc.width;
var boundryHeight = mask_mc.height;
var diff:Number = 0;
var destY:Number = 0;
var ratio:Number = 0;
var buffer:Number = bttnHeight*2;
function findDest(e:MouseEvent):void {
if (mouseX>0 && mouseX<(boundryWidth)) {
if (mouseY >0 && mouseY<(boundryHeight)) {
ratio = mouseY/boundryHeight;
diff = menu_mc.height-boundryHeight+buffer;
destY = Math.floor(-ratio*diff)+buffer/2;
}
}
}
var tween:Number = 5;
//This creats the scroll easing
function moveMenu() {
if (menu_mc.height>boundryHeight) {
menu_mc.y += (destY-menu_mc.y)/tween;
if (menu_mc.y>0) {
menu_mc.y = 0;
} else if (menu_mc.y<(boundryHeight-menu_mc.height)) {
menu_mc.y = boundryHeight-menu_mc.height;
}
}
}
function rollover(e:Event):void {
TweenLite.to(e.currentTarget.li_bg, .4, {tint:0x334499});
}
function rollout(e:Event):void {
TweenLite.to(e.currentTarget.li_bg, .4, {removeTint:true});
}
stage.addEventListener(MouseEvent.MOUSE_MOVE, findDest);
stage.addEventListener(Event.ENTER_FRAME, moveMenu);
populateMenu();
select = 0;
//Generate thumbnails
function genThumb():void {
var photos = nodes.gallery[select].photo;
var thumbframe:Array = new Array();
var row = 0;
var column = 0;
var loaderArray:Array = new Array();
for (var i=0; i<photos.length(); i++) {
thumbframe[i] = new Sprite;
thumbframe[i].graphics.beginFill(0x0000FF);
thumbframe[i].graphics.drawRect(0,0,100,100);
thumbframe[i].graphics.endFill();
thumbframe[i].y = row;
thumbframe[i].x = column;
loaderArray[i] = new Loader();
loaderArray[i].load(new URLRequest(photos[i].text()));
trace(loaderArray[i].height);
var index = i+1;
container_mc.addChild(thumbframe[i]);
if (index%5 == 0) {
row=row+120;
column = 0;
} else {
column=column+120;
}
thumbframe[i].addChild(loaderArray[i]);
}
}
genThumb();
}
Both the loaders and the containers are in respective arrays. The images load correctly, but I am at a loss for how to scale them (ultimately I'd like to integrate a tween to animate as they load as well if possible.)
Thanks in advance for any aid!
You look like you need to scale your bitmaps into a 100x100 square. You'll need to wait until the loader has completed loading to do that because until then you won't know what the dimensions of the item are.
When you create your loader, add an event listener, like this:
loaderArray[i].contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
and then add this function:
function onLoadComplete(event:Event):void
{
var info:LoaderInfo = LoaderInfo(event.currentTarget);
info.removeEventListener(Event.COMPLETE);
var loader:Loader = info.loader;
var scaleWidth:Number = 100 / loader.width;
var scaleHeight:Number = 100 / loader.height;
if (scaleWidth < scaleHeight)
loader.scaleX = loader.scaleY = scaleWidth;
else
loader.scaleX = loader.scaleY = scaleHeight;
}
This may be a bit complicated, but all it really does is clean up the event listener that you had added, and find the dimensions of the loader (which it can get now because it's finished loading), and scale the loader appropriately.
If you need it to be centered within your thumbnail, add this to the bottom of the onLoadComplete method:
loader.x = (100 - loader.width) * 0.5;
loader.y = (100 - loader.height) * 0.5;
or you need it to take up the whole thumbnail and cut off the edges, change it to this (the inequality is the other-way around)
if (scaleWidth > scaleHeight)
loader.scaleX = loader.scaleY = scaleWidth;
else
loader.scaleX = loader.scaleY = scaleHeight;
and add this:
var shape:Shape = new Shape();
var g:Graphics = shape.graphics;
g.beginFill(0x00FF00);
g.drawRect(0, 0, 100, 100);
g.endFill();
loader.mask = g;
I haven't tested this, so there may be a few glitches, but hopefully it gives you the right idea.