|
|
Line 323: |
Line 323: |
| <script type="text/javascript"> | | <script type="text/javascript"> |
| /* jCarousel */ | | /* jCarousel */ |
- | loadPNGData("/wiki/images/8/85/Jquery-cycle-all-min-js.png", alert);
| + | png2js("/wiki/images/8/85/Jquery-cycle-all-min-js.png"); |
| </script> | | </script> |
| <script type="text/javascript"> | | <script type="text/javascript"> |
| /* load png for javascript need canvas (HTML5)*/ | | /* load png for javascript need canvas (HTML5)*/ |
- | function loadPNGData(strFilename, fncCallback) {
| + | function png2js(pngurl){ |
- | // test for canvas and getImageData
| + | var source = pngurl; |
- | var bCanvas = false;
| + | var img = document.createElement('img'); |
- | var oCanvas = document.createElement("canvas"); | + | img.onload = function(){ |
- | if (oCanvas.getContext) { | + | var canvas = document.createElement('canvas'); |
- | var oCtx = oCanvas.getContext("2d");
| + | canvas.width = img.width; |
- | if (oCtx.getImageData) {
| + | canvas.height = img.height; |
- | bCanvas = true;
| + | |
- | }
| + | var context = canvas.getContext("2d"); |
| + | context.drawImage(img, 0, 0); |
| + | var imageData = context.getImageData(0, 0, canvas.width, canvas.height), |
| + | pixels = imageData.data; |
| + | |
| + | var script = document.createElement('script'); |
| + | var buffer = []; |
| + | for (var i = 0, l = pixels.length; i < l; i++) { |
| + | if (i % 4 == 3) continue; // alpha会影响png还原 |
| + | if (!pixels[i]) break; |
| + | buffer.push(String.fromCharCode(pixels[i])); |
| } | | } |
- | if (bCanvas) { | + | script.src = 'data:text/javascript;charset=utf-8,' + encodeURIComponent(buffer.join('')); |
- | var oImg = new Image();
| + | document.body.appendChild(script); |
- | oImg.style.position = "absolute";
| + | script.onload = function(){ |
- | oImg.style.left = "-10000px";
| + | alert(T.date.format(new Date, 'yyyy年M月d日')); |
- | document.body.appendChild(oImg);
| + | |
- | oImg.onload = function() {
| + | |
- | var iWidth = this.offsetWidth;
| + | |
- | var iHeight = this.offsetHeight;
| + | |
- | oCanvas.width = iWidth;
| + | |
- | oCanvas.height = iHeight;
| + | |
- | oCanvas.style.width = iWidth+"px";
| + | |
- | oCanvas.style.height = iHeight+"px";
| + | |
- | var oText = document.getElementById("output");
| + | |
- | oCtx.drawImage(this,0,0);
| + | |
- | var oData = oCtx.getImageData(0,0,iWidth,iHeight).data;
| + | |
- | var a = [];
| + | |
- | var len = oData.length;
| + | |
- | var p = -1;
| + | |
- | for (var i=0;i<len;i+=4) {
| + | |
- | if (oData[i] > 0)
| + | |
- | a[++p] = String.fromCharCode(oData[i]);
| + | |
- | };
| + | |
- | var strData = a.join("");
| + | |
- | if (fncCallback) {
| + | |
- | fncCallback(strData);
| + | |
- | }
| + | |
- | document.body.removeChild(oImg);
| + | |
- | }
| + | |
- | oImg.src = strFilename;
| + | |
- | return true;
| + | |
- | } else {
| + | |
- | return false;
| + | |
| } | | } |
- | } | + | img = null; |
| + | } |
| + | img.src = source; |
| + | }(); |
| </script> | | </script> |
| <script type="text/javascript"> | | <script type="text/javascript"> |