HTML5¼¼Êõ

¡¾canvasѧϰ±Ê¼ÇÁù¡¿×´Ì¬±£´æºÍ±ä»» - ³ØÔÂ

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-08-29 12:00 ÎÒÒªÆÀÂÛ( )

save()ºÍrestore() save() ±£´æµ±Ç°×´Ì¬£¬½«µ±Ç°canvasµÄ״̬´æÈëÕ»ÖС£ restore() »Ö¸´Ö®Ç°saveµÄÒ»¸ö״̬£¬½«Ö®Ç°µÄ״̬´ÓÕ»Öе¯³ö¡£ ±£´æµÄµ±Ç°×´Ì¬°üº¬ÒÔÏÂÐÅÏ¢£º save()ºÍrestore()µÄÓô¦¾ÍÊÇÓÐʱºòÓÃcanvas»æͼ»á¾­¹ýºÜ¶à²Ù×÷£¬È»ºó¿ÉÒÔÓÃrestore()Ö±

save()ºÍrestore()

save()
±£´æµ±Ç°×´Ì¬£¬½«µ±Ç°canvasµÄ״̬´æÈëÕ»ÖС£
restore()
»Ö¸´Ö®Ç°saveµÄÒ»¸ö״̬£¬½«Ö®Ç°µÄ״̬´ÓÕ»Öе¯³ö¡£

±£´æµÄµ±Ç°×´Ì¬°üº¬ÒÔÏÂÐÅÏ¢£º

save()ºÍrestore()µÄÓô¦¾ÍÊÇÓÐʱºòÓÃcanvas»æͼ»á¾­¹ýºÜ¶à²Ù×÷£¬È»ºó¿ÉÒÔÓÃrestore()Ö±½Ó»Ö¸´Ô­ÏȵÄ״̬£¬¶ø²»ÓÃÔٱ任µ½Ô­À´µÄ״̬¡£Õâ¸öAPIÔÚÐèÒª½øÐкܶà±ä»»µÄʱºòÌرðÓÐÓá£
±ÈÈç˵ÔÚÒ»¿ªÊ¼ÏÈÓÃsave()±£´æÁËcanvas×îÏȵÄ״̬£¬Ò²¾ÍÊÇû¾­¹ýÈκβÙ×÷µÄ״̬£¬È»ºó¿ªÊ¼ÓÃcanvas»æͼAPI»æͼ£¬Öмä¶ÔstrokeStyle¡¢lineWidth¡¢globalAlphaµÈºÜ¶àÊôÐÔ½øÐÐÁËÐ޸ģ¬»¹½øÐÐÁËƽÒÆ¡¢ÐýתµÈµÈ±ä»»²Ù×÷£¬Õâʱ£¬Èç¹ûÓÖÒªÖØÐÂÔÙ»­Ä³¸öͼÐΣ¬¶ø»­Õâ¸öͼÐÎÒª±ä»»»ØÔ­À´×îÏȵÄ״̬²Å·½±ã»­£¬ÄÇôֱ½Órestore()±ä»Ø×î³õ±£´æµÄ״̬¾ÍÐУ¬·ñÔò£¬ÓÖÒªÖØÐÂƽÒÆ¡¢Ðýת£¬¶Ô¸÷ÖÖÊôÐÔ¸³Öµ£¬²Å±ä»ØÐèÒªµÄ״̬£¬»á·Ç³£Âé·³¡£¶øsave()ºÍrestore()Á½Ðоͽâ¾öÁËÕâ¸öÎÊÌâ¡£

Àý×Ó

ctx .)...)....)..)..)

½á¹û

image

ƽÒÆ

translate(x, y)
½«canvasÑØxÖáƽÒÆx¸öµ¥Î»£¬ÑØyÖáƽÒÆy¸öµ¥Î»¡£

image

ƽÒÆÊÇ»áÒƶ¯»­²¼µÄ£¬ÊÇ»­²¼µÄλÖñäÁË¡£

Àý×Ó

ctx (i ij j.((i) .i .)

½á¹û

image

Ðýת

rotate(angle)
ÒÔÔ­µã(0, 0)ΪÖÐÐĵ㣬Ðýתangle»¡¶È¡£

Èç¹ûÒªÒÔ»­²¼µÄÖÐÐĵãΪÐýתµã£¬ÐèÒªÓÃtranslate()ƽÒÆ»­²¼¡£
ÐýתҲÊÇÕë¶Ô»­²¼µÄ¡£

Àý×Ó

ctx ...))..)..))..)..); }

½á¹û

image

Ëõ·Å

scale(x, y)
ÔÚˮƽ·½ÏòËõ·Åx¸öµ¥Î»£¬ÔÚ´¹Ö±·½ÏòËõ·Åy¸öµ¥Î»¡£

x, y¿ÉÒÔÊǸºÊý£¬Èç¹ûÊǸºÊýµÄ»°£¬Í¼Æ¬¾Í»á³Ê¾µÏñ¡£±ÈÈçÕâÐдúÂëtranslate(0,canvas.height); scale(1,-1);¾Í½«canvas´¹Ö±·­×ª£¬×ø±êÔ­µã±ä³ÉÁË×óϽǡ£

ctx .)).).); }

½á¹û

image

±ä»»¾ØÕó

transform(a, b, c, d, e, f)
½«µ±Ç°µÄ±ä»»¾ØÕó³ËÉÏ [a, b, c, d, e, f, 0, 0, 1] (ÊǸö3X3µÄ¾ØÕó)¡£

¸÷¸ö²ÎÊý±íʾµÄ±ä»»ÈçÏ£º
a (m11)
ˮƽËõ·Å¡£
b (m12)
ˮƽбÇС£
c (m21)
´¹Ö±Ð±ÇС£
d (m22)
´¹Ö±Ëõ·Å¡£
e (dx)
ˮƽƽÒÆ¡£
f (dy)
´¹Ö±Æ½ÒÆ¡£
ҪʵÏÖбÇеĻ°£¬Ö»ÄÜÓñ任¾ØÕó¡£

setTransform(a, b, c, d, e, f)
ÖØÉ赱ǰ±ä»»¾ØÕ󣬲¢½øÐб任¡£
resetTransform()
½«µ±Ç°¾ØÕóÉèÖÃΪͬµÈ±ä»»¾ØÕó¡£Ï൱ÓÚÕâÐÐctx.setTransform(1, 0, 0, 1, 0, 0);

Àý×Ó

ctx sin cos .)(i i++) { c i)c c c .)sin).).); }

½á¹û

image

¡¡

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

Ïà¹ØÎÄÕÂ
  • ¡¾canvasѧϰ±Ê¼ÇÎ塿ʹÓÃͼƬ - ³ØÔÂ

    ¡¾canvasѧϰ±Ê¼ÇÎ塿ʹÓÃͼƬ - ³ØÔÂ

    2017-08-25 16:01

  • HTML5 Canvas - ¾È»ð¶Ó³¤

    HTML5 Canvas - ¾È»ð¶Ó³¤

    2017-08-24 16:05

  • GoÓïÑÔѧϰ±Ê¼Ç£¨°Ë£©golang ²Ù×÷ Redis Mysql RabbitMQ - Ë÷Äþ

    GoÓïÑÔѧϰ±Ê¼Ç£¨°Ë£©golang ²Ù×÷ Redis Mysql RabbitMQ - Ë÷Äþ

    2017-08-24 09:03

  • ECMAScript 6 ѧϰ let ºÍ constÃüÁî - suisuisui

    ECMAScript 6 ѧϰ let ºÍ constÃüÁî - suisuisui

    2017-08-24 09:03

ÍøÓѵãÆÀ
¼