{"id":363,"date":"2020-07-27T09:06:08","date_gmt":"2020-07-27T00:06:08","guid":{"rendered":"https:\/\/blog.wsd.sh\/?p=363"},"modified":"2020-07-27T09:06:08","modified_gmt":"2020-07-27T00:06:08","slug":"14-15-javascript-%e5%a4%89%e6%9b%b4","status":"publish","type":"post","link":"https:\/\/blog.wsd.sh\/?p=363","title":{"rendered":"14->15 JavaScript \u5909\u66f4"},"content":{"rendered":"<p>ES6\u306e\u8a18\u6cd5<br \/>\n\u30fbfunction ()\u3000\uff0d\uff1e\u3000() =><\/p>\n<p>1.1 \/\/ \u30d5\u30a1\u30f3\u30af\u30b7\u30e7\u30f3\u3092\u30b0\u30eb\u30fc\u30d4\u30f3\u30b0<br \/>\nbrfore:<\/p>\n<pre>\r\n        \/\/ ccanvas\u306e\u521d\u671f\u5316\r\n        this.init = function () {\r\n                this.canvas = document.getElementById('canvas');\r\n                this.ctx = this.canvas.getContext('2d');\r\n\r\n                this.setBgColor(this.bgColor);\r\n                this.canvas.onmousedown = function(e){\r\n                \/\/ \u30de\u30a6\u30b9\u62bc\u4e0b\u958b\u59cb\r\n                        this.drawStart(e.offsetX, e.offsetY, this.cnvBold, this.cnvColor);\r\n                        this.canvas.addEventListener(\"mousemove\" ,this.mouseMove, false);\r\n                }.bind(this);\r\n                this.canvas.onmouseup = function(){\r\n                \/\/ \u30de\u30a6\u30b9\u62bc\u4e0b\u7d42\u4e86\r\n                        this.canvas.removeEventListener(\"mousemove\" , this.mouseMove, false);\r\n                }.bind(this);\r\n        };\r\n\r\n    \/\/ canvas\u4e0a\u3067\u306e\u30a4\u30d9\u30f3\u30c8\r\n        this.mouseMove = function (e){\r\n                \/\/console.log(this);\r\n                this.draw(e.offsetX, e.offsetY, this.cnvBold, this.cnvColor);\r\n        }.bind(this);\r\n\r\n        this.drawStart = function (x, y, cnvBold, cnvColor ) {\r\n                this.ctx.lineWidth = cnvBold;\r\n                this.ctx.strokeStyle = 'rgba('+cnvColor+')';\r\n                \/\/ \u521d\u56de\u51e6\u7406\r\n                this.ctx.beginPath();\r\n                this.ctx.lineCap = \"round\";  \/\/\u3000\u7dda\u3092\u89d2\u4e38\u306b\u3059\u308b\r\n                this.ctx.moveTo(x, y);\r\n        };\r\n\r\n        \/\/ \u63cf\u753b\u51e6\u7406\r\n        this.draw = function (x, y, cnvBold, cnvColor ) {\r\n                this.ctx.lineWidth = cnvBold;\r\n                this.ctx.strokeStyle = 'rgba('+cnvColor+')';\r\n                \/\/ \u521d\u56de\u4ee5\u964d\u51e6\u7406\r\n                this.ctx.lineTo(x, y);\r\n                this.ctx.stroke();\r\n        };\r\n\r\n        \/\/ canvas\u306e\u80cc\u666f\u8272\u3092\u8a2d\u5b9a(\u6307\u5b9a\u304c\u306a\u3044\u5834\u5408\u306bjpeg\u4fdd\u5b58\u3059\u308b\u3068\u80cc\u666f\u304c\u9ed2\u306b\u306a\u308b)\r\n        this.setBgColor = function( bgColor){\r\n                this.ctx.fillStyle = bgColor;\r\n                this.ctx.fillRect(0, 0, this.cnvWidth, this.cnvHeight);\r\n        };\r\n\r\n        this.setCnvColor = function (cnvColor) {\r\n                this.cnvColor = cnvColor;\r\n        };\r\n\r\n        this.setCnvBold = function (cnvBold) {\r\n                this.cnvBold = cnvBold;\r\n        };\r\n<\/pre>\n<p>after:<\/p>\n<pre>\r\n        \/\/ ccanvas\u306e\u521d\u671f\u5316\r\n        this.init = () => {\r\n                this.canvas = document.getElementById('canvas');\r\n                this.ctx = this.canvas.getContext('2d');\r\n\r\n                this.setBgColor(this.bgColor);\r\n\r\n                this.canvas.onmousedown = (e) => {\r\n                \/\/ \u30de\u30a6\u30b9\u62bc\u4e0b\u958b\u59cb\r\n                        this.drawStart(e.offsetX, e.offsetY, this.cnvBold, this.cnvColor);\r\n                        this.canvas.addEventListener(\"mousemove\" ,this.mouseMove, false);\r\n                }\r\n                this.canvas.onmouseup = () => {\r\n                \/\/ \u30de\u30a6\u30b9\u62bc\u4e0b\u7d42\u4e86\r\n                        this.canvas.removeEventListener(\"mousemove\" , this.mouseMove, false);\r\n                }\r\n        };\r\n\r\n    \/\/ canvas\u4e0a\u3067\u306e\u30a4\u30d9\u30f3\u30c8\r\n        this.mouseMove = (e) => {\r\n                \/\/console.log(this);\r\n                this.draw(e.offsetX, e.offsetY, this.cnvBold, this.cnvColor);\r\n        }\r\n\r\n        this.drawStart = (x, y, cnvBold, cnvColor ) => {\r\n                this.ctx.lineWidth = cnvBold;\r\n                this.ctx.strokeStyle = 'rgba('+cnvColor+')';\r\n                \/\/ \u521d\u56de\u51e6\u7406\r\n                this.ctx.beginPath();\r\n                this.ctx.lineCap = \"round\";  \/\/\u3000\u7dda\u3092\u89d2\u4e38\u306b\u3059\u308b\r\n                this.ctx.moveTo(x, y);\r\n        };\r\n\r\n        \/\/ \u63cf\u753b\u51e6\u7406\r\n        this.draw = (x, y, cnvBold, cnvColor )  => {\r\n                this.ctx.lineWidth = cnvBold;\r\n                this.ctx.strokeStyle = 'rgba('+cnvColor+')';\r\n                \/\/ \u521d\u56de\u4ee5\u964d\u51e6\u7406\r\n                this.ctx.lineTo(x, y);\r\n                this.ctx.stroke();\r\n        };\r\n\r\n        \/\/ canvas\u306e\u80cc\u666f\u8272\u3092\u8a2d\u5b9a(\u6307\u5b9a\u304c\u306a\u3044\u5834\u5408\u306bjpeg\u4fdd\u5b58\u3059\u308b\u3068\u80cc\u666f\u304c\u9ed2\u306b\u306a\u308b)\r\n        this.setBgColor = ( bgColor) => {\r\n                this.ctx.fillStyle = bgColor;\r\n                this.ctx.fillRect(0, 0, this.cnvWidth, this.cnvHeight);\r\n        };\r\n\r\n        this.setCnvColor = (cnvColor) => {\r\n                this.cnvColor = cnvColor;\r\n        };\r\n\r\n        this.setCnvBold = (cnvBold) => {\r\n                this.cnvBold = cnvBold;\r\n        };\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ES6\u306e\u8a18\u6cd5 \u30fbfunction ()\u3000\uff0d\uff1e\u3000() => 1.1 \/\/ \u30d5\u30a1\u30f3\u30af\u30b7\u30e7\u30f3\u3092\u30b0\u30eb\u30fc\u30d4\u30f3\u30b0 brfore: \/\/ ccanvas\u306e\u521d\u671f\u5316 this.init = function () { this.canva&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/363"}],"collection":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=363"}],"version-history":[{"count":1,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/363\/revisions"}],"predecessor-version":[{"id":364,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/363\/revisions\/364"}],"wp:attachment":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}