HTML5¼¼Êõ

»ñÈ¡Canvasµ±Ç°×ø±êϵ¾ØÕó - ·½Ë§

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£º²©¿ÍÔ° 2016-07-10 16:00 ÎÒÒªÆÀÂÛ( )

Ç°ÑÔ ¾ßÌå´úÂë Ê×ÏÈÇë¿´ÏÂÃæµÄÒ»¶Î´úÂ루ÏÂÎľßÌå½âÊÍ´úÂë×÷Óã©£º 1 window.TrackTransform = function () { 2 var svg = document.createElementNS("http://www.w3.org/2000/svg", 'svg' ); 3 var xform = svg.createSVGMatrix(); 4 var savedTransforms =

Ç°ÑÔ ¾ßÌå´úÂë

Ê×ÏÈÇë¿´ÏÂÃæµÄÒ»¶Î´úÂ루ÏÂÎľßÌå½âÊÍ´úÂë×÷Óã©£º  

1 window.TrackTransform = function () { 2 var svg = document.createElementNS("http://www.w3.org/2000/svg", 'svg'); 3 var xform = svg.createSVGMatrix(); 4 var savedTransforms = []; 5 this.trackTransform=function(ctx) { 6 7 ctx.getTransform = function () { return xform; }; save = ctx.save; 10 ctx.save = function () { 11 savedTransforms.push(xform.translate(0, 0)); 12 return save.call(ctx); 13 }; 14 var restore = ctx.restore; 15 ctx.restore = function () { 16 xform = savedTransforms.pop(); 17 return restore.call(ctx); 18 }; scale = ctx.scale; 21 ctx.scale = function (sx, sy) { 22 xform = xform.scaleNonUniform(sx, sy); 23 return scale.call(ctx, sx, sy); 24 }; 25 var rotate = ctx.rotate; 26 ctx.rotate = function (deg) { radians = deg * Math.PI / 180; 29 xform = xform.rotate(deg); 30 return rotate.call(ctx, radians); 31 }; 32 var translate = ctx.translate; 33 ctx.translate = function (dx, dy) { 34 xform = xform.translate(dx, dy); 35 return translate.call(ctx, dx, dy); 36 }; 37 var transform = ctx.transform; 38 ctx.transform = function (a, b, c, d, e, f) { 39 var m2 = svg.createSVGMatrix(); 40 m2.a = a; m2.b = b; m2.c = c; m2.d = d; m2.e = e; m2.f = f; 41 xform = xform.multiply(m2); 42 return transform.call(ctx, a, b, c, d, e, f); 43 }; 44 var setTransform = ctx.setTransform; 45 ctx.setTransform = function (a, b, c, d, e, f) { 46 xform.a = a; 47 xform.b = b; 48 xform.c = c; 49 xform.d = d; 50 xform.e = e; 51 xform.f = f; 52 return setTransform.call(ctx, a, b, c, d, e, f); 53 }; 54 var pt = svg.createSVGPoint(); ctx.transformedPoint = function (x, y) { 57 pt.x = x; pt.y = y; 58 return pt.matrixTransform(xform.inverse()); 59 } 60 var pt2 = svg.createSVGPoint(); ctx.transformedPoint2 = function (x, y) { 63 pt2.x = x; pt2.y = y; 64 return pt2.matrixTransform(xform); 65 } 66 var clearRect = ctx.clearRect; 67 ctx.clearRect = function (x, y, w, h) { 68 ctx.save(); 69 ctx.setTransform(1, 0, 0, 1, 0, 0); 70 clearRect.call(ctx, x, y, w, h); 71 ctx.restore(); 72 } 73 } 74 }

´úÂëÖÐÖ÷Òª¶¨ÒåÁËÒ»¸öÀàTrackTransform£¬ÖØдÁËCanvasRenderingContext2D¶ÔÏóµÄsave£¬restore£¬scale£¬rotate£¬translate£¬transform£¬setTransform£¬clearRect·½·¨¡£

TrackTransformÀàʹÓÃ

ÈçºÎʹÓÃwindow.TrackTransformÀàÄØ£¿Í¨¹ýÒÔÏÂÁ½¾ä´úÂ룬±äÁ¿contexµÄת»»·½·¨¼´½øÐÐÁËÖØд¡£

track = new TrackTransform(); 3 track.trackTransform(context);

·½·¨Ïê½â

¾ßÌå½âÊÍÉÏÃæ¸÷ÐдúÂëµÄ×÷ÓÃ

1 ´´½¨¾ØÕó¶ÔÏóxform 2 var svg = document.createElementNS("http://www.w3.org/2000/svg", 'svg'); 3 var xform = svg.createSVGMatrix();

µÚ2ÐдúÂëͨ¹ýcreateElementNS´´½¨Óë»ñÈ¡µ½SVG¶ÔÏó¡£

µÚ3ÐдúÂëͨ¹ýcreateSVGMatrix()·½·¨´´½¨²¢·µ»ØÒ»¸öеÄ2x3µÄ¾ØÕóSVGMatrix¾ØÕó¶ÔÏó¸³Öµµ½xform¡£

ÎÒÃÇÔÚä¯ÀÀÆ÷Öдò¿ª¿ª·¢Õß¹¤¾ß£¬µ½¿ØÖÆ̨¿ÉÒÔÊä³ö¾ØÕó¿´¿´³õʼֵ¡£

a b c d e f Õâ6¸öÖµ¾Í¶ÔÓ¦ÁËÎÒÃÇÔÚ½éÉÜtransform·½·¨µÄÄÇ6¸ö²ÎÊý¡£Õâ¸ö2x3µÄ¾ØÕóΪÁË·½±ã¾ØÕóÔËËãÎÒÃÇ°ÑËüÀ©Õ¹ÎªÒ»¸ö3x3µÄ¾ØÕó¡£

svgMatrix½éÉÜ

ÕâÀïÔÙ¼òµ¥½âÊÍÏÂsvgMatrix£º

svgMatrixµÄ·½·¨ºÍÊôÐÔÈçÏÂͼ£¨¾ßÌå¿É²ÎÔÄ£ºhttps://developer.mozilla.org/en-US/docs/Web/API/SVGMatrix£©

 

2 save·½·¨ savedTransforms.push(xform.translate(0, 0)); return save.call(ctx);

ÏȽ«xform´æ´¢ÔÚÒ»¸öÊý×ésavedTransformsÖУ¬È»ºóµ÷ÓÃÔ­API·½·¨¡£

3 restore·½·¨ xform = savedTransforms.pop(); return restore.call(ctx);

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • canvas֪ʶµã - H.U.C-Íõ×Ó

    canvas֪ʶµã - H.U.C-Íõ×Ó

    2017-04-16 10:02

  • ʹÓÃphonegap»ñÈ¡É豸µÄһЩÐÅÏ¢ - ganchuanpu

    ʹÓÃphonegap»ñÈ¡É豸µÄһЩÐÅÏ¢ - ganchuanpu

    2017-03-30 13:00

  • canvasʵÏֺڿ͵۹ú¾ØÐÎÕó - ·çÓêºó¼û²Êºç

    canvasʵÏֺڿ͵۹ú¾ØÐÎÕó - ·çÓêºó¼û²Êºç

    2017-03-30 09:00

  • canvas»­Ö±½Ç×ø±êϵ - shbwh-tswq

    canvas»­Ö±½Ç×ø±êϵ - shbwh-tswq

    2017-03-18 10:02

ÍøÓѵãÆÀ
¸