I recently started to learn html5. Because I have always studied as, I still think as is more pleasing to the eye, but I have to learn html5, so I figured out that I can write html5 using the syntax of as, which is suitable for making games. It's easier. Let's start the first article
The first article shows a picture
1, code comparison
as code:
public var loader:Loader; public function loadimg():void{ loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,complete); loader.load(new URLRequest("10594855.png")); } public function complete(event:Event):void{ var image:Bitmap = Bitmap(loader.content); var bitmap:BitmapData = image.bitmapData; addChild(image); }
js code:
window.onload = function(){ var canvas = document.getElementById("myCanvas"); var context = canvas.getContext("2d"); image = new Image(); image.onload = function(){ context.drawImage(image, 0, 0, 240, 240); }; image.src = "10594855.png"; };
Two, write the code after js class library (tentatively named legend library)
var loader; function main(){ loader = new LLoader(); loader.addEventListener(LEvent.COMPLETE,loadBitmapdata); loader.load("10594855.png","bitmapData"); } function loadBitmapdata(event){ var bitmapdata = new LBitmapData(loader.content); var bitmap = new LBitmap(bitmapdata); addChild(bitmap); }
Three, implement
1, build a static class, convenient Save and access public method attributes, such as canvas, etc.
var LGlobal = function (){} LGlobal.type = "LGlobal";
We always use canvas, so we need to save the canvas and add properties and methods to the LGlobal class
LGlobal.canvas = null; LGlobal.width = 0; LGlobal.height = 0; LGlobal.setCanvas = function (id,width,height){ var canvasObj = document.getElementById(id); if(width)canvasObj.width = width; if(height)canvasObj.height = height; LGlobal.width = canvasObj.width; LGlobal.height = canvasObj.height; LGlobal.canvas = canvasObj.getContext("2d"); }
Interface In order to be able to display dynamically, choose to constantly refresh the canvas
Add a continuous refresh method to the LGlobal class
LGlobal.onShow = function (){ if(LGlobal.canvas == null)return; LGlobal.canvas.clearRect(0,0,LGlobal.width,LGlobal.height); }
Then, I envision saving all real objects in an array , and then display them in order
And all objects that can be displayed have a show method to draw themselves on the canvas
The LGlobal class was last modified to
var LGlobal = function (){} LGlobal.type = "LGlobal"; LGlobal.canvas = null; LGlobal.width = 0; LGlobal.height = 0; LGlobal.childList = new Array(); LGlobal.setCanvas = function (id,width,height){ var canvasObj = document.getElementById(id); if(width)canvasObj.width = width; if(height)canvasObj.height = height; LGlobal.width = canvasObj.width; LGlobal.height = canvasObj.height; LGlobal.canvas = canvasObj.getContext("2d"); } LGlobal.onShow = function (){ if(LGlobal.canvas == null)return; LGlobal.canvas.clearRect(0,0,LGlobal.width,LGlobal.height); LGlobal.show(LGlobal.childList); } LGlobal.show = function(showlist){ var key; for(key in showlist){ if(showlist[key].show){ showlist[key].show(); } } }
2, in as, the picture The two classes BitmapData and Bitmap are used for display. In order to realize the functions of these two classes, we create two classes LBitmapData and LBitmap respectively
Let's first look at the LBitmapData class. The LBitmapData class is used to store image data, etc. We store Image() in the LBitmapData class
function LBitmapData(image,x,y,width,height){ var self = this; self.type = "LBitmapData"; self.oncomplete = null; if(image){ self.image = image; self.x = (x==null?0:x); self.y = (y==null?0:y); self.width = (width==null?self.image.width:width); self.height = (height==null?self.image.height:height); }else{ self.x = 0; self.y = 0; self.width = 0; self.height = 0; self.image = new Image(); } }
Looking at the LBitmap class, the LBitmap class is used to display the Image() stored in the LBitmapData class
function LBitmap(bitmapdata){ var self = this; self.type = "LBitmap"; self.x = 0; self.y = 0; self.width = 0; self.height = 0; self.scaleX=1; self.scaleY=1; self.visible=true; self.bitmapData = bitmapdata; if(self.bitmapData){ self.width = self.bitmapData.width; self.height = self.bitmapData.height; } }
About Image() For display, we use the show method mentioned at the beginning, which is implemented as follows
LBitmap.prototype = { show:function (){ var self = this; if(!self.visible)return; LGlobal.canvas.drawImage(self.bitmapData.image, self.bitmapData.x,self.bitmapData.y,self.bitmapData.width,self.bitmapData.height, self.x,self.y,self.width*self.scaleX,self.height*self.scaleY); } }
3. Finally, there is still a Loader missing. We create our own LLoader class
function LLoader(){ var self = this; self.loadtype = ""; self.content = null; self.oncomplete = null; self.event = {}; } LLoader.prototype = { addEventListener:function(type,listener){ var self = this; if(type == LEvent.COMPLETE){ self.oncomplete = listener; } }, load:function (src,loadtype){ var self = this; self.loadtype = loadtype; if(self.loadtype == "bitmapData"){ self.content = new Image(); self.content.onload = function(){ if(self.oncomplete){ self.event.currentTarget = self.content; self.oncomplete(self.event); } } self.content.src = src; } } }
4 , the LEvent class is used in 3. The LEvent class is an event class used to load events, clicks, etc. This will be gradually strengthened in the future
var LEvent = function (){}; LEvent.COMPLETE = "complete"; LEvent.ENTER_FRAME = "enter_frame"; LEvent.currentTarget = null; LEvent.addEventListener = function (node, type, fun){ if(node.addEventListener){ node.addEventListener(type, fun, false); }else if(node.attachEvent){ node['e' + type + fun] = fun; node[type + fun] = function(){node['e' + type + fun]();} node.attachEvent('on' + type, node[type + fun]); } }
5. Before displaying, we need to have an The addChild method is as follows
function addChild(DisplayObject){ LGlobal.childList.push(DisplayObject); }
6. Finally, after the entire page is read, the image can be displayed
function init(speed,canvasname,width,height,func){ LEvent.addEventListener(window,"load",function(){ setInterval(function(){LGlobal.onShow();}, speed); LGlobal.setCanvas(canvasname,width,height); func(); }); } init(40,"back",300,300,main); var loader; function main(){ loader = new LLoader(); loader.addEventListener(LEvent.COMPLETE,loadBitmapdata); loader.load("10594855.png","bitmapData"); } function loadBitmapdata(event){ var bitmapdata = new LBitmapData(loader.content); var bitmap = new LBitmap(bitmapdata); addChild(bitmap); }
The above is the syntax of imitating ActionScript Let's write html5 - the first article, display the content of an image. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!