{"id":2406,"date":"2013-01-24T17:22:19","date_gmt":"2013-01-24T08:22:19","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=2406"},"modified":"2013-12-29T18:51:35","modified_gmt":"2013-12-29T09:51:35","slug":"post-2406","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/d3-js\/2406","title":{"rendered":"\u3010D3.js\u3011\u753b\u50cf\u3092\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3068\u3057\u3066\u4f7f\u3046"},"content":{"rendered":"<p><a href=\"http:\/\/shimz.me\/example\/d3js\/d3canvas\/\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/01\/54e63f844254d9c9d85b02a36349db2f.jpg\" alt=\"\u5206\u89e3\" width=\"1158\" height=\"206\" class=\"aligncenter size-full wp-image-2408\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/01\/54e63f844254d9c9d85b02a36349db2f.jpg 1158w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/01\/54e63f844254d9c9d85b02a36349db2f-300x53.jpg 300w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/01\/54e63f844254d9c9d85b02a36349db2f-768x137.jpg 768w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/01\/54e63f844254d9c9d85b02a36349db2f-1024x182.jpg 1024w\" sizes=\"auto, (max-width: 1158px) 100vw, 1158px\" \/><\/a><\/p>\n<p>\u8aad\u307f\u8fbc\u3093\u3060\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092canvas\u306b\u8ee2\u5199\u3057\u3001getImageData\u3067\u53d6\u5f97\u3057\u305fcanvasPixelArray\u3092\u6574\u5f62\u3057\u3066\u3001d3\u3078\u6e21\u3057\u307e\u3059\u3002<br \/>\n\u5f8c\u306f\u53d7\u3051\u3068\u3063\u305f\u30c7\u30fc\u30bf\u3092\u5143\u306bsvg\u306ecircle\u3067\u63cf\u753b\u3002<br \/>\n\u8a73\u7d30\u306f<a href=\"http:\/\/shimz.me\/example\/d3js\/d3canvas\/\">example<\/a>\u306e\u30bd\u30fc\u30b9\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u30dc\u30bf\u30f3\u30af\u30ea\u30c3\u30af\u3067\u3001\u8272\u60c5\u5831\u3092\u5143\u306b\u5206\u89e3\u3000\u2192\u3000\u3042\u3093\u307e\u308a\u7dba\u9e97\u306b\u5206\u89e3\u3067\u304d\u305a orz <\/p>\n<pre class=\"lang:js decode:true \" >var w = 100;\r\nvar h = 100;\r\n\r\n\/\/canvas \u8a2d\u7f6e\r\nd3.select('body').append('canvas')\r\n  .attr('id', 'canvas')\r\n  .attr('width', w)\r\n  .attr('heigh', h)\r\n  .attr('style', 'display:none')\r\n\r\nvar canvas = document.getElementById('canvas');\r\nvar ctx = canvas.getContext('2d');\r\n\r\n\/\/\u30a4\u30e1\u30fc\u30b8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u4f5c\u6210\r\nvar imgObj = new Image();\r\nimgObj.src = \"Parrots.jpg\";\r\n\r\n\r\nimgObj.onload = function() {   \/\/\u753b\u50cf\u306e\u8aad\u307f\u8fbc\u307f\r\n\t\r\n\tctx.drawImage(imgObj, 0, 0); \/\/canvas\u306b\u8ee2\u5199\r\n\t\r\n\tvar imgData = ctx.getImageData(0, 0, w, h); \/\/ImageData\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u53d6\u5f97\r\n\r\n\r\n\tvar imgDataArray  = [];\r\n\tfor(var i=0; i &lt; imgData.width * imgData.height * 4 ; i+=4){\t\r\n\t\t\/\/canvasPixelArray\u3092\u6574\u5f62\u3057\u305f\u30c7\u30fc\u30bf\u3092imgDataArray\u306b\r\n\t\timgDataArray.push( {\r\n\t\t\tr:imgData.data[i],\r\n\t\t\tg:imgData.data[i+1],\r\n\t\t\tb:imgData.data[i+2],\r\n\t\t\tcolor:(function(r,g,b){return ((((0&lt;&lt;8)+r&lt;&lt;8)+g&lt;&lt;8)+b)})(imgData.data[i], imgData.data[i+1], imgData.data[i+2])\r\n\t\t} ); \r\n\t\t\r\n\t\t\r\n\t}\r\n\r\n\tvar svg = d3.select('body').append('svg'); \/\/svg\u30bf\u30b0\u8a2d\u7f6e\r\n\t\r\n\tsvg.selectAll(\"circle\")\r\n\t\t.data(imgDataArray)  \/\/canvas\u304b\u3089\u53d6\u5f97\u3057\u305f\u30d4\u30af\u30bb\u30eb\u30c7\u30fc\u30bf\r\n\t\t.enter()\r\n\t\t.append(\"svg:circle\")\r\n\t\t.attr('cx', function(d, i){ return (i % w) * 2})\r\n\t\t.attr('cy', function(d, i){ return Math.floor(i \/ w) * 2})\r\n\t\t.attr('fill', function(d, i){ return  \"#\" + d.color.toString(16)})\r\n\t\t.attr('r', 2)\r\n\t\r\n}<\/pre>\n<p><a href=\"http:\/\/shimz.me\/example\/d3js\/d3canvas\/\">example<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8aad\u307f\u8fbc\u3093\u3060\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092canvas\u306b\u8ee2\u5199\u3057\u3001ge&hellip;<\/p>\n","protected":false},"author":1,"featured_media":2408,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-2406","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-d3-js","has-post-thumbnail-archive"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/comments?post=2406"}],"version-history":[{"count":8,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2406\/revisions"}],"predecessor-version":[{"id":2413,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2406\/revisions\/2413"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media\/2408"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=2406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=2406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=2406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}