HTML5¼¼Êõ

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

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

´ÓsavedTransformsÊý×éÖÐÈ¥³ý×îºóÒ»¸ö¶ÔÏ󣬲¢½«Æ丳ֵµ½±äÁ¿xform£¬È»ºóµ÷ÓÃÔ­API·½·¨¡£ 4 scale·½·¨xform = xform.scaleNonUniform(sx, sy);return scale.call(ctx, sx, sy); ά»¤xform£¬½«ÆäËõ·Å£¬È»ºóµ÷ÓÃÔ­API

´ÓsavedTransformsÊý×éÖÐÈ¥³ý×îºóÒ»¸ö¶ÔÏ󣬲¢½«Æ丳ֵµ½±äÁ¿xform£¬È»ºóµ÷ÓÃÔ­API·½·¨¡£

4 scale·½·¨ xform = xform.scaleNonUniform(sx, sy); return scale.call(ctx, sx, sy);

 Î¬»¤xform£¬½«ÆäËõ·Å£¬È»ºóµ÷ÓÃÔ­API·½·¨¡£

5 rotate·½·¨ ¡¡¡¡¡¡¡¡¡¡¡¡ var radians = deg * Math.PI / 180; xform = xform.rotate(deg); return rotate.call(ctx, radians);

 ½«½Ç¶ÈתΪ»¡¶È£¬Î¬»¤xform½«Æä½øÐÐÐýת±ä»»£¬È»ºóµ÷ÓÃÔ­API·½·¨¡£

6 translate·½·¨ xform = xform.translate(dx, dy); return translate.call(ctx, dx, dy);

 Î¬»¤xform½«Æä½øÐÐƽÒƱ任£¬È»ºóµ÷ÓÃÔ­API·½·¨¡£

7 transform·½·¨ var m2 = svg.createSVGMatrix(); m2.a = a; m2.b = b; m2.c = c; m2.d = d; m2.e = e; m2.f = f; xform = xform.multiply(m2); return transform.call(ctx, a, b, c, d, e, f); Ê×ÏÈÉùÃ÷Ò»¸öеľØÕóm2£¬m2¸³ÖµÎªÒª½øÐб任µÄ6¸ö²ÎÊýÖµ£¬È»ºóxformºÍm2Ö´ÐоØÕó³Ë·¨ÔËË㣬ÔËËã½á¹û¸³Öµµ½xform½«Æäά»¤¡£È»ºóµ÷ÓÃÔ­API·½·¨¡£ 8 setTransform·½·¨ xform.a = a; xform.b = b; xform.c = c; xform.d = d; xform.e = e; xform.f = f; return setTransform.call(ctx, a, b, c, d, e, f);

ά»¤xformµÄÖµ£¬È»ºóµ÷ÓÃÔ­API·½·¨¡£

9 clearRect·½·¨ ctx.save(); ctx.setTransform(1, 0, 0, 1, 0, 0); clearRect.call(ctx, x, y, w, h); ctx.restore();

Ê×Ïȱ£´æcontextµÄµ±Ç°×´Ì¬£¬½«»­²¼ÖØÖõ½Ô­Ê¼×´Ì¬£¨¿ÉÒÔÀí½âΪ×ø±êϵÖØÖõ½Ä¬ÈÏ×ø±êϵ£©£¬È»ºóµ÷ÓÃÔ­API·½·¨Çå³ý»­²¼Ö¸¶¨·¶Î§ÄÚÈÝ¡£Çå³ýºóµ÷ÓÃrestore»Ö¸´Canvas֮ǰ±£´æµÄ״̬¡£

10 getTeansform·½·¨ ctx.getTransform = function () { return xform; };

½ÓÏÂÀ´½éÉܵÄÈý¸ö·½·¨¶¼ÊÇÔ­APIûÓеġ£getTeansformÖ±½Ó·µ»Øxform£¬¿ÉÒÔ¿´µ½´ú±í»­²¼¾ØÕóµÄ6¸öÖµabcdef¡£

11 transformedPoint·½·¨ 54 var pt = svg.createSVGPoint(); 55 //ͨ¹ýÔ­×ø±êϵµãx£¬yÇó¶ÔÓ¦µ±Ç°×ø±êϵµÄ×ø±êÖµ 56 ctx.transformedPoint = function (x, y) { 57 pt.x = x; pt.y = y; 58 return pt.matrixTransform(xform.inverse()); 59 }

ͨ¹ýÔ­×ø±êϵµãx£¬yÇó¶ÔÓ¦µ±Ç°×ø±êϵµÄ×ø±êÖµ¡£

createSVGPoint´´½¨µÄµãΪ£¨0,0£©£¬xform.inverse()ÊÇÇóxformµÄÄæ¾ØÕó¡£MatrixTransformÔòÊÇͨ¹ýÒ»ÖÖ¾ØÕóËã·¨À´½øÐÐÔËËãµÃµ½ÏàÓ¦µÄ±äÐεÄЧ¹ûµÄ¡£¾ØÕóµÄһЩ»ù±¾Ëã·¨¾Í²»¶à×ܽáÁË£¬ÒÔÇ°ÉϿξÍѧ¹ýÁË£¬ÍøÉÏÒ²Óв»ÉÙ½²½â¡£

12 transformedPoint2·½·¨ 60 var pt2 = svg.createSVGPoint(); 61 //µ±Ç°×ø±êϵÖеĵÄxy»¹Ô­µ½Ô­×ø±êϵ×ø±êÖµ 62 ctx.transformedPoint2 = function (x, y) { 63 pt2.x = x; pt2.y = y; 64 return pt2.matrixTransform(xform); 65 }

µ±Ç°×ø±êϵÖеĵÄx£¬y»¹Ô­µ½Ô­×ø±êϵ×ø±êÖµ¡£

 

¡¡

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

ÍøÓѵãÆÀ
ý