Core Knowledge Point: DrawImage
Role: Loading pictures on canvas
Html:
<id= "MyCanvas" width= "Max" height= " style = "border:1px solid #c3c3c3;" > Your Browser does not support the canvas element. </ Canvas >
Js:
<script type= "Text/javascript" >var c=document.getelementbyid ("MyCanvas"); var cxt=c.getcontext ("2d"); var img=New Image () img.src= "Flower.png"cxt.drawimage (img,0,0); </script>
Mobile Image Loading Instance
Requirements: You need to add a load effect to a picture in a list
Html
< Sectionclass= "Productul"ID= "Productul"> <ul> <Li> <ahref="#"> <Divclass= "Triangle-topleft"></Div> <spanclass= "Shuxing"Data_url= "productinfo.html">Exclusive</span> <Divclass= "Leftimages fl"><Canvasdata-src= "Images/product/product1.png" ></Canvas></Div> <Divclass= "Productcontent fr"> <Pclass= "Ptitle pl10">Title</P> <Pclass= "PDEs pl10">Introduction Here Introduction here Introduction here Introduction here Introduction here Introduction here Introduction here Introduction here Intro</P> <Pclass= "Pprice pl10">Price:<spanclass= "Green">¥5000</span></P> </Div> </a> </Li> </ul> </ Section>
Focus CSS
img {width:100px; birder:0;} Canvas {width:100px; min-height:100px; background:#fff url (".. /images/loading.gif ") Center center no-repeat; background-size:15px auto; }
Js
varImglength = $ ("#productul"). Find ("Canvas"). length; if(Imglength > 0) { $("#productul"). Find ("Canvas"). each (function () { varIMGSRC = $ ( This). Data ("src"); varImageobj =NewImage (); Imageobj.canvs= $( This) [0]; varCVS = ImageObj.canvs.getContext (' 2d '); if(CVS) {imageobj.onload=function() {imageObj.canvs.width= This. Width; ImageObj.canvs.height= This. Height; Cvs.drawimage ( This, 0, 0); $ (Imageobj.canvs). CSS ("Background-image", "none"); } imageobj.src=imgsrc; } }) } }
Canvas Mobile tips frequently used image loading