{"id":353,"date":"2020-07-25T10:44:17","date_gmt":"2020-07-25T01:44:17","guid":{"rendered":"https:\/\/blog.wsd.sh\/?p=353"},"modified":"2020-07-25T10:44:52","modified_gmt":"2020-07-25T01:44:52","slug":"10-11-htmljavascript-%e5%a4%89%e6%9b%b4-module%e3%81%ae%e5%b0%8e%e5%85%a5","status":"publish","type":"post","link":"https:\/\/blog.wsd.sh\/?p=353","title":{"rendered":"10->11 html,JavaScript \u5909\u66f4 module\u306e\u5c0e\u5165"},"content":{"rendered":"<p>module\u306e\u5c0e\u5165<br \/>\n1. index.html<br \/>\n1.1 module\u306e\u5c0e\u5165\u306e\u70ba\u3001htlm\u304b\u3089\u306ejavaScript\u306e\u547c\u3073\u51fa\u3057\u540d\u3092\u5909\u3048\u308b<br \/>\nbefore:<\/p>\n<pre>\r\n&lt;script type=\"module\" src=\"js\/canvas.js\"&gt;&lt;\/script&gt;\r\n<\/pre>\n<p>after:<\/p>\n<pre>\r\n&lt;script type=\"module\" src=\"js\/main.js\"&gt;&lt;\/script&gt;\r\n<\/pre>\n<p>1.2 js\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u914d\u7f6e<br \/>\njs&ensp;&#8212;-&ensp;main.js<br \/>\n&ensp;&ensp;&ensp;&ensp;|<br \/>\n&ensp;&ensp;&ensp;&ensp;-&ensp;modules&ensp;&#8212;&ensp;canvas.js<\/p>\n<pre>\r\n$ pwd\r\n\/var\/www\/html\/JS\/11\/js\r\n$ ls\r\nmain.js  modules\r\n$ cd modules\/\r\n$ ls\r\ncanvas.js\r\n$\r\n<\/pre>\n<p>1.3 main.js<\/p>\n<pre>\r\nimport { rootCreate} from  '.\/modules\/canvas.js';\r\n\r\nvar root =  new rootCreate();\r\n\r\nwindow.root = root;\r\n\r\nwindow.onload = init;\r\n\r\nfunction init () {\r\n    \/\/ canvas\r\n\r\n\r\n        root.init();\r\n\r\n    \/\/ \u8272\u306e\u5909\u66f4\r\n        let cElements = document.getElementsByClassName('color');\r\n        var c_elms;\r\n        for (let i = 0; i < cElements.length; i++) {\r\n                c_elms = cElements[i].childNodes;\r\n\r\n                for (let j = 0; j < c_elms.length; j++) {\r\n                        if(c_elms[j].tagName != 'A') {\r\n                                continue;\r\n                        }\r\n                        c_elms[j].onclick = function() {\r\n                                let cnvColor = this.getAttribute( \"data-color\" ) ;\r\n                                console.log(\"cnvColor =\"+cnvColor );\r\n\r\n                                root.setCnvColor(cnvColor);\r\n                        }\r\n                }\r\n        }\r\n\r\n\r\n    \/\/ \u7dda\u306e\u592a\u3055\u5909\u66f4\r\n        let bElements = document.getElementsByClassName('bold');\r\n        var b_elms;\r\n        for (let i = 0; i < bElements.length; i++) {\r\n                b_elms = bElements[i].childNodes;\r\n\r\n                for (let j = 0; j < b_elms.length; j++) {\r\n                        if(b_elms[j].tagName != 'A') {\r\n                                continue;\r\n                        }\r\n                        b_elms[j].onclick = function() {\r\n                                let cnvBold = this.getAttribute( \"data-bold\" ) ;\r\n                                console.log(\"cnvBold =\"+cnvBold );\r\n\r\n                                root.setCnvBold(cnvBold);\r\n                        }\r\n                }\r\n        }\r\n\r\n\r\n    \/\/ \u63cf\u753b\u30af\u30ea\u30a2\r\n        var clear = document.getElementById(\"clear\");\r\n        clear.onclick = function(){\r\n                root.ctx.clearRect(0, 0, root.cnvWidth, root.cnvHeight);\r\n                root.setBgColor(root.bgColor);\r\n        }\r\n\r\n    \/\/ canvas\u3092\u753b\u50cf\u3067\u4fdd\u5b58\r\n        var download = document.getElementById(\"download\");\r\n        download.onclick = function(){\r\n                var base64 = root.canvas.toDataURL(\"image\/jpeg\");\r\n                document.getElementById(\"download\").href = base64;\r\n        }\r\n}\r\n<\/pre>\n<p>1.4 canvas.js<\/p>\n<pre>\r\nfunction rootCreate () {\r\n        \/\/ context\r\n        this.ctx = null;\r\n        this.canvas = null;\r\n        this.cnvWidth = 500;\r\n        this.cnvHeight = 500;\r\n    \/\/ \u5909\u6570\u5ba3\u8a00\r\n        this.cnvColor = \"255, 0, 0, 1\";  \/\/ \u7dda\u306e\u8272\r\n        this.cnvBold = 5;  \/\/ \u7dda\u306e\u592a\u3055\r\n        this.bgColor = \"transparent\";\r\n\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                        let root = window.root;\r\n                \/\/ \u30de\u30a6\u30b9\u62bc\u4e0b\u958b\u59cb\r\n                        root.drawStart(e.offsetX, e.offsetY, root.cnvBold, root.cnvColor);\r\n                        root.canvas.addEventListener(\"mousemove\" ,root.mouseMove, false);\r\n                }\r\n                this.canvas.onmouseup = function(){\r\n                        let root = window.root;\r\n                \/\/ \u30de\u30a6\u30b9\u62bc\u4e0b\u7d42\u4e86\r\n                        root.canvas.removeEventListener(\"mousemove\" , root.mouseMove, false);\r\n                }\r\n        };\r\n\r\n    \/\/ canvas\u4e0a\u3067\u306e\u30a4\u30d9\u30f3\u30c8\r\n        this.mouseMove = function (e){\r\n                let root = window.root;\r\n                \/\/console.log(this);\r\n                root.draw(e.offsetX, e.offsetY, root.cnvBold, root.cnvColor);\r\n        };\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\r\n        return this;\r\n};\r\n\r\nexport { rootCreate };\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>module\u306e\u5c0e\u5165 1. index.html 1.1 module\u306e\u5c0e\u5165\u306e\u70ba\u3001htlm\u304b\u3089\u306ejavaScript\u306e\u547c\u3073\u51fa\u3057\u540d\u3092\u5909\u3048\u308b before: &lt;script type=&#8221;module&#8221; src=&#8221;js\/c&#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\/353"}],"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=353"}],"version-history":[{"count":2,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/353\/revisions"}],"predecessor-version":[{"id":355,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/353\/revisions\/355"}],"wp:attachment":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}