{"id":2356,"date":"2017-05-21T10:31:16","date_gmt":"2017-05-21T02:31:16","guid":{"rendered":"http:\/\/www.qdabc.cn\/?p=2356"},"modified":"2017-05-21T10:31:16","modified_gmt":"2017-05-21T02:31:16","slug":"canvas-%e7%88%b1%e5%bf%83","status":"publish","type":"post","link":"http:\/\/www.qdabc.cn\/?p=2356","title":{"rendered":"canvas \u7231\u5fc3"},"content":{"rendered":"<pre>&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta charset=\"utf-8\"&gt;\r\n&lt;meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"&gt;\r\n&lt;title&gt;HTML5&lt;\/title&gt;\r\n&lt;style&gt;\r\n*{padding:0;margin:0;}   \r\ncanvas {\r\n    display: block;\r\n    background: #000;\r\n    cursor: crosshair;\r\n    display: block;\r\n}\r\n&lt;\/style&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;canvas id=\"c\"&gt;&lt;\/canvas&gt;\r\n&lt;script&gt;\r\nwindow.requestAnimFrame =\r\n    window.requestAnimationFrame ||\r\n    window.webkitRequestAnimationFrame ||\r\n    window.mozRequestAnimationFrame ||\r\n    window.oRequestAnimationFrame ||\r\n    window.msRequestAnimationFrame ||\r\n    function (callback) {\r\n        window.setTimeout(callback, 1000 \/ 60);\r\n};\r\nvar windW  = document.documentElement.clientWidth || document.body.clientWidth,\r\n    windH  = document.documentElement.clientHeight ||document.body.clientHeight;\r\n\r\n    canvas = document.getElementById('c');\r\n    context = canvas.getContext('2d');\r\n\r\n    canvas.width = windW;\r\n    canvas.height = windH;\r\n\r\n    originX = windW\/2;\r\n    originY = windH;\r\n\r\n\r\n    var firesBox = firesBoxTmp = [],\r\n        line = 20;    \r\n        \r\n    var Fireworks = function(px,sx,line,fire,dely){\r\n\r\n        this.fire = fire;\r\n        this.dely = dely;\r\n        this.endX = px.x;\r\n        this.endY = px.y;\r\n\r\n        this.startX = sx.x;\r\n        this.startY = sx.y;\r\n\r\n        this.p1 = {\r\n            x:sx.x,\r\n            y:sx.y\r\n        };\r\n        var totallong = Math.sqrt((this.endX-this.p1.x) * (this.endX-this.p1.x) +(this.endY-this.p1.y) * (this.endY-this.p1.y));\r\n\r\n        this.p2 = {\r\n            x:(line*(this.endX-this.p1.x))\/totallong+this.p1.x,\r\n            y:(line*(this.endY-this.p1.y))\/totallong+this.p1.y\r\n        }\r\n\r\n        this.color = {\r\n            r:Math.round(Math.random()*255),\r\n            g:Math.round(Math.random()*255),\r\n            b:Math.round(Math.random()*255),\r\n            a:Math.random()\r\n        }\r\n    }\r\n\r\n    Fireworks.prototype.draw = function(){\r\n        context.beginPath();\r\n        context.moveTo(this.p1.x, this.p1.y);\r\n        context.lineTo(this.p2.x, this.p2.y);\r\n      \/\/  context.strokeStyle =  'hsla(' + this.color.r + ', '+this.color.g+'%, ' + this.color.b + '%, ' + this.color.a + ')';\r\n        context.strokeStyle =  'rgba(' + this.color.r + ', '+this.color.g+', ' + this.color.b + ', ' + this.color.a + ')';\r\n        context.stroke();\r\n        context.closePath();\r\n    }\r\n\r\n    Fireworks.prototype.update = function(){\r\n        if(\r\n            (this.endX&lt;this.startX&amp;&amp;this.p1.x&lt;this.endX)||\r\n            (this.endX&gt;=this.startX&amp;&amp;this.p1.x&gt;=this.endX)\r\n        ){\r\n            if(!this.fire) firesBoxTmp.push(this);\r\n            return ;\r\n        }\r\n        var dely  = this.dely;\r\n        this.p1.x += (this.endX-this.startX)*dely;\r\n        this.p1.y += (this.endY-this.startY)*dely;\r\n        this.p2.x += (this.endX-this.startX)*dely;\r\n        this.p2.y += (this.endY-this.startY)*dely;\r\n\r\n        this.color.r+=5;\r\n\r\n        if(\r\n            (this.endX&lt;this.startX&amp;&amp;this.p1.x&gt;=this.endX)||\r\n            (this.endX&gt;=this.startX&amp;&amp;this.p1.x&lt;=this.endX)\r\n        ){\r\n           firesBoxTmp.push(this);\r\n        }else{\r\n            firesBoxTmp.push(this);\r\n            if(!this.fire){ \r\n                firesBoxTmp.push(this);\r\n                return ;\r\n            }\r\n            var startP = {},endP={};\r\n\r\n            startP.x = this.endX;\r\n            startP.y = this.endY;\r\n\r\n            for (var i=1;i&lt;10;i++) {\r\n                if(i!=5){\r\n                    r = Math.random()*10;\r\n                    endP.x = startP.x + Math.sin(2 * Math.PI * i * 36 \/ 360) * r;\r\n                    endP.y = startP.y - Math.cos(2 * Math.PI * i * 36 \/ 360) * r;\r\n                    fire = new Fireworks(endP,startP,Math.random()*line*.5,false,.013);\r\n                    firesBoxTmp.push(fire);\r\n                }\r\n                \r\n            }  \r\n        }\r\n    }\r\n    function update(){\r\n        requestAnimFrame(update);\r\n        context.clearRect(0, 0, canvas.width, canvas.height);  \r\n        var i = firesBox.length;\r\n        if (i &lt;= 0) return;\r\n        while (i--) {\r\n            firesBox[i].draw();\r\n            firesBox[i].update();\r\n        }\r\n        firesBox = firesBoxTmp;\r\n        firesBoxTmp = [];\r\n        \r\n    }\r\n\r\n    function heartShape(){\r\n        if(heartindex&gt;=4.8){\r\n            clearTimeout(heart);\r\n        }\r\n        var endP ={} ,startP = {},r=90,dx=windW\/2,dy=windH\/3;\r\n\r\n        startP.x = originX;\r\n        startP.y = originY;\r\n\r\n        var m,n,x,y,i= heartindex;\r\n        \r\n        m = i;\r\n        n = -r * (((Math.sin(i) * Math.sqrt(Math.abs(Math.cos(i)))) \/ (Math.sin(i) + 1.4)) - 2 * Math.sin(i) + 2);\r\n        endP.x = n * Math.cos(m) + dx;\r\n        endP.y = n * Math.sin(m) + dy;\r\n        fire = new Fireworks(endP,startP,line,true,0.016);\r\n        firesBox.push(fire);\r\n        heartindex+=.04;\r\n        \r\n    }\r\n    var heartindex = -1.56;\r\n    var heart = setInterval(function(){\r\n        heartShape();\r\n    },10);\r\n    window.onload = update;\r\n    heartShape();\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=2356\">canvas \u7231\u5fc3<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset=&#8221;utf-8&#8243;&gt; &lt;meta http-equiv=&#8221;X- [&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,336],"_links":{"self":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2356"}],"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=2356"}],"version-history":[{"count":1,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2356\/revisions"}],"predecessor-version":[{"id":2357,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2356\/revisions\/2357"}],"wp:attachment":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2356"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}