{"id":2368,"date":"2017-05-24T10:54:38","date_gmt":"2017-05-24T02:54:38","guid":{"rendered":"http:\/\/www.qdabc.cn\/?p=2368"},"modified":"2017-05-24T10:54:38","modified_gmt":"2017-05-24T02:54:38","slug":"canvas%e4%ba%ba%e7%89%a9%e5%8a%a8%e4%bd%9c%ef%bc%8c%e6%8c%89%e7%a9%ba%e6%a0%bc%e9%94%ae%e5%bc%80%e5%a7%8b","status":"publish","type":"post","link":"http:\/\/www.qdabc.cn\/?p=2368","title":{"rendered":"canvas\u4eba\u7269\u52a8\u4f5c\uff0c\u6309\u7a7a\u683c\u952e\u5f00\u59cb"},"content":{"rendered":"<pre>&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\r\n    &lt;meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\"&gt;\r\n    &lt;title&gt;03-\u5b9e\u73b0baby\u6848\u4f8b&lt;\/title&gt;\r\n    &lt;style&gt;\r\n        canvas {\r\n            border: 1px solid #000;\r\n        }\r\n    &lt;\/style&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;canvas id=\"canvas\" width=\"600\" height=\"400\"&gt;&lt;\/canvas&gt;\r\n    &lt;!--&lt;script src=\".\/My-solider.js\"&gt;&lt;\/script&gt;--&gt;\r\n    &lt;script&gt;\r\n\r\n        function Solider(options){\r\n            \/*\u5c5e\u6027*\/\r\n            this.width=options.width||40;\r\n            this.height=options.height||65;\r\n            this.frame=options.frame||0;\r\n            this.direction=options.direction||0;\r\n            this.imgSrc=options.imgSrc;\r\n            this.image=new Image();\r\n            this.x=options.x||0;\r\n            this.y=options.y||0;\r\n            this.step=options.step||1;\r\n            this.context=options.context;\r\n            \/*\u5b9a\u65f6\u5668*\/\r\n            this.timer=null;\r\n            \/*\u5224\u65ad\u662f\u5426\u5728\u8d70ing*\/\r\n            this.isWalking=false;\r\n            \/*\u521d\u59cb\u5316*\/\r\n            this.init();\r\n        }\r\n\r\n        \/*\u65b9\u6cd5*\/\r\n        Solider.prototype={\r\n            constructor:Solider,\r\n            \/*\u521d\u59cb\u5316\u52a8\u753b*\/\r\n            init:function(){\r\n                var that=this;\r\n                \/*\u56fe\u7247\u52a0\u8f7d\u5b8c\u6210\u540e\uff0c\u7ed1\u5b9a\u4e8b\u4ef6\u5e76\u7ed8\u5236\u4e00\u6b21\u56fe\u7247*\/\r\n                this.image.addEventListener('load',function(){\r\n                    that.bind();\r\n                    that.draw();\r\n                })\r\n                \/*\u521d\u59cb\u5316\u52a8\u753b*\/\r\n                this.image.src=this.imgSrc;\r\n            },\r\n            \/*\u66f4\u65b0\u72b6\u6001*\/\r\n            update:function(){\r\n                \/*\u83b7\u53d6canvas\u7684\u5bbd\u548c\u9ad8*\/\r\n                var w=this.context.canvas.width,\r\n                        h=this.context.canvas.height;\r\n\r\n                \/*1 \u66f4\u65b0\u5e27*\/\r\n                this.frame= ++this.frame%4;\r\n\r\n                \/*\u66f4\u65b0\u4eba\u7269\u4f4d\u7f6e*\/\r\n\r\n                switch (this.direction){\r\n                    case 0:\/\/down\r\n                        this.y+=this.step;\r\n                        if(this.y&gt;=h){\r\n                            this.y=-this.height-10;\r\n                        }\r\n                        break;\r\n                    case 1:\/\/left\r\n                        this.x-=this.step;\r\n                        if(this.x&lt;=-this.width){\r\n                            this.x=w+10;\r\n                        }\r\n                        break;\r\n                    case 2:\/\/right\r\n                        this.x+=this.step;\r\n                        if(this.x&gt;=w){\r\n                            this.x=-this.width-10;\r\n                        }\r\n                        break;\r\n                    case 3:\/\/up\r\n                        this.y-=this.step;\r\n                        if(this.y&lt;=-this.height){\r\n                            this.y=h+10;\r\n                        }\r\n                        break;\r\n                }\r\n            },\r\n            \/*\u5f00\u59cb\u52a8\u753b*\/\r\n            start:function(){\r\n                var that=this;\r\n                this.isWalking=true;\r\n                this.timer=setInterval(function(){\r\n                    that.update();\r\n                    that.draw();\r\n                },80)\r\n\r\n            },\r\n            \/*\u505c\u6b62\u52a8\u753b*\/\r\n            stop:function(){\r\n                this.isWalking=false;\r\n                window.clearInterval(this.timer);\r\n            },\r\n            \/*\u4e8b\u4ef6\u7ed1\u5b9a*\/\r\n            bind:function(){\r\n                var that=this;\r\n                window.document.addEventListener('keydown',function(e){\r\n                    \/*\u83b7\u53d6keycode*\/\r\n                    var keycode= e.keyCode;\r\n                    console.log(keycode);\r\n                    \/*\u5224\u65ad\u662f\u5426\u6309\u4e0b\u7a7a\u683c\u952e*\/\r\n                    if(keycode==32){\r\n                        \/*\u5224\u65ad\u5f53\u524d\u662f\u5426\u5728\u8fd0\u52a8*\/\r\n                        if(that.isWalking){\r\n                            that.stop();\r\n                        }else{\r\n                            that.start();\r\n                        }\r\n                    }\r\n                    \/*\u5982\u679c\u52a8\u753b\u5df2\u7ecf\u505c\u6b62\u4e86\uff0c\u90a3\u4e48\u4e0b\u9762\u7684\u4ee3\u7801\u4e0d\u9700\u8981\u6267\u884c*\/\r\n                    if(!that.isWalking){\r\n                        return;\r\n                    }\r\n                    \/*\u5982\u679c\u6b63\u5728\u6267\u884c\u52a8\u753b\uff0c\u53bb\u76d1\u542c\u952e\u76d8\u3002\u6539\u53d8\u65b9\u5411*\/\r\n                    switch (keycode){\r\n                        case 38:\r\n                        case 87:  \/\/up\r\n                            that.direction=3;\r\n                            break;\r\n\r\n                        case 39:\r\n                        case 68:  \/\/right\r\n                            that.direction=2;\r\n                            break;\r\n\r\n                        case 40:\r\n                        case 83:  \/\/down\r\n                            that.direction=0;\r\n                            break;\r\n\r\n                        case 37:\r\n                        case 65:  \/\/left\r\n                            that.direction=1;\r\n                            break;\r\n                    }\r\n\r\n                })\r\n            },\r\n            \/*\u6839\u636e\u72b6\u6001\u7ed8\u5236\u52a8\u753b*\/\r\n            draw:function(){\r\n                var context=this.context;\r\n                var w=context.canvas.width,\r\n                        h=context.canvas.height;\r\n                \/*1 \u6e05\u9664\u753b\u5e03*\/\r\n                context.clearRect(0,0,w,h);\r\n                \/*\u7ed8\u5236\u5c0f\u4eba\u56fe\u7247*\/\r\n                context.drawImage(this.image,this.frame*this.width,this.direction*this.height,this.width,this.height,\r\n                        this.x,this.y,this.width,this.height);\r\n\r\n            }\r\n\r\n        }\r\n\r\n        \/*\u6d4b\u8bd5*\/\r\n        new Solider({\r\n            imgSrc:'http:\/\/cdn.attach.qdfuns.com\/notes\/pics\/201705\/23\/230433jwz0t1ee201amut9.png',\r\n            context:document.getElementById('canvas').getContext('2d'),\r\n            step:5\r\n        })\r\n    &lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p class=\"post-copyright\">\u6b22\u8fce\u5206\u4eab\u672c\u6587\uff0c\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u51fa\u5904\uff1a<a href=\"http:\/\/www.qdabc.cn\">\u524d\u7aefABC<\/a> &raquo; <a href=\"http:\/\/www.qdabc.cn\/?p=2368\">canvas\u4eba\u7269\u52a8\u4f5c\uff0c\u6309\u7a7a\u683c\u952e\u5f00\u59cb<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>&lt;!DOCTYPE html&gt; &lt;html lang=&#8221;en&#8221;&gt; &lt;head&gt; &lt;meta charset=&#8221;UTF-8&#8243;&gt; &lt;meta name [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[30,9],"_links":{"self":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2368"}],"collection":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2368"}],"version-history":[{"count":1,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2368\/revisions"}],"predecessor-version":[{"id":2369,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2368\/revisions\/2369"}],"wp:attachment":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2368"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}