{"id":3174,"date":"2013-12-04T09:53:17","date_gmt":"2013-12-04T00:53:17","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=3174"},"modified":"2017-03-14T15:06:35","modified_gmt":"2017-03-14T06:06:35","slug":"post-3174","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/d3-js\/3174","title":{"rendered":"D3.js\u3068Fabric.js\u3092\u4f7f\u3063\u3066canvas\u306b\u63cf\u753b\u3059\u308b"},"content":{"rendered":"<p><a href=\"http:\/\/www.adventar.org\/calendars\/117\">d3.js Advent Calendar 2013<\/a> 4\u65e5\u76ee\u306e\u53c2\u52a0\u8a18\u4e8b\u3067\u3059\u3002<\/p>\n<p>\u4ee5\u524d\u3082\u3001Fabric.js\u306b\u3064\u3044\u3066\u306f\u66f8\u3044\u305f\u3053\u3068\u304c\u3042\u308b\u306e\u3067\u3059\u304c\u3001D3\u3068Fabric\u306e\u7d44\u307f\u5408\u308f\u305b\u306b\u306f\u3044\u308d\u3044\u308d\u306a\u53ef\u80fd\u6027\u3092\u611f\u3058\u308b\u306e\u3067\u3001\u5e03\u6559\u306e\u3064\u3082\u308a\u3067\u6539\u3081\u3066\u8a18\u4e8b\u306b\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>Fabric\u306f\u3001HTML5\u306ecanvas\u8981\u7d20\u3092\u4f7f\u3044\u3084\u3059\u304f\u3059\u308bJavaScript\u306e\u30b0\u30e9\u30d5\u30a3\u30c3\u30af\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<\/p>\n<blockquote><p>\u6700\u8fd1\u3067\u306f\u3001Canvas \u306f\u3001\u79c1\u305f\u3061\u304c\u3001\u5168\u304f \u9a5a\u304f\u3079\u304d \u30b0\u30e9\u30d5\u30a3\u30c3\u30af\u30b9 \u3092\u30a6\u30a7\u30d6\u4e0a\u3067\u4f5c\u308b\u3053\u3068\u3092\u53ef\u80fd\u306b\u3057\u307e\u3057\u305f\u3002\u3057\u304b\u3057\u3001 Canvas\u306eAPI\u304c\u63d0\u4f9b\u3059\u308b\u306e\u306f\u3001\u6b8b\u5ff5\u306a\u304c\u3089\u4f4e\u6c34\u6e96\u3067\u3059\u3002 \u3082\u3057\u3001\u79c1\u305f\u3061\u304c\u5358\u306b\u57fa\u672c\u7684\u306a\u56f3\u5f62\u3092 canvas\u4e0a\u306b\u307b\u3068\u3093\u3069\u63cf\u304d\u305f\u3044\u3068\u601d\u308f\u305a\u3001\u305d\u306e\u3053\u3068\u3092\u5fd8\u308c\u3066\u3057\u307e\u3048\u3070\u5225\u3067\u3059\u304c\u3001 \u3069\u3093\u306a\u5f62\u306b\u305b\u3088\u30a4\u30f3\u30bf\u30e9\u30af\u30b7\u30e7\u30f3\u304c\u5fc5\u8981\u306b\u306a\u3063\u305f\u308a\u3001 \u3044\u304b\u306a\u308b\u70b9\u3067\u3042\u308c\u3001\u7d75\u306e\u5909\u66f4\u304c\u5fc5\u8981\u306b\u306a\u3063\u305f\u308a\u3001\u3082\u3063\u3068\u8907\u96d1\u306a\u56f3\u5f62\u3092 \u63cf\u304f\u3053\u3068\u304c\u5fc5\u8981\u306b\u306a\u3063\u305f\u3089\u3001\u72b6\u6cc1\u306f\u4e00\u5909\u3057\u307e\u3059\u3002<\/p>\n<p>Fabric\u306f\u3001\u3053\u306e\u554f\u984c\u306e\u89e3\u6c7a\u3092\u76ee\u6307\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n(\u4e2d\u7565)<br \/>\n\u305d\u306e\u3088\u3046\u306a\u4f4e\u6c34\u6e96\u3067\u4ed5\u4e8b\u3092\u3059\u308b\u4ee3\u308f\u308a\u306b\u3001Fabric \u306f\u3001\u7c21\u5358\u3067\u3059\u304c\u3001\u30cd\u30a4\u30c6\u30a3\u30d6\u30e1\u30bd\u30c3\u30c9\u4e0a\u306b\u5f37\u529b\u306a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30e2\u30c7\u30eb\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002 Fabric\u306f\u3001canvas\u306e\u72b6\u614b\u3068\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u306e\u9762\u5012\u3092\u898b\u3066\u304f\u308c\u3001\u76f4\u63a5\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6271\u3046\u4ed5\u4e8b\u3092\u3055\u305b\u3066\u304f\u308c\u307e\u3059\u3002\n<\/p><\/blockquote>\n<p><a href=\"http:\/\/fabricjs.com\/\">Fabric.js \u516c\u5f0f<\/a><br \/>\n<span class=\"removed_link\" title=\"http:\/\/yamineko.lv9.org\/Fabric.js%20Javascript%20Canvas%20Library\/Fabric.js%20Javascript%20Canvas%20Library.html\">Fabric.js \u65e5\u672c\u8a9e<\/span><\/p>\n<p>\u3053\u306e\u624b\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u305d\u308c\u3053\u305d\u5c71\u306e\u3088\u3046\u306b\u3042\u308a\u307e\u3059\u304c\u3001Fabric\u306e\u7279\u5fb4\u306f\u5185\u90e8\u306bSVG\u30d1\u30fc\u30b5\u30fc\u3092\u5185\u8535\u3057\u3066\u304a\u308asvg\u3067\u63cf\u304b\u308c\u305f\u56f3\u3092canvas\u306b\u8ee2\u5199\u3059\u308b\u6a5f\u80fd\u306b\u3059\u3050\u308c\u3066\u3044\u308b\u70b9\u3067\u3059\u3002<\/p>\n<p><a href=\"https:\/\/gunmagisgeek.com\/blog\/d3-js\/2892\">[Fabric.js]SVG\u3092canvas\u306b\u8ee2\u5199\u3059\u308b<\/a><\/p>\n<p>\u4e0a\u8a18\u8a18\u4e8b\u306e\u3088\u3046\u306b\u306bFabric\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001D3\u3067\u4f5c\u6210\u3057\u305fsvg\u3082\u7c21\u5358\u306bcanvs\u306b\u8ee2\u5199\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u307e\u305fFabric\u3067\u306f\u3001SVG\u306epath\u30a8\u30ec\u30e1\u30f3\u30c8\u540c\u69d8\u306bpathLength\u30b3\u30de\u30f3\u30c9\u3092\u7528\u3044\u3066canvas\u4e0a\u306b\u30d1\u30b9\u3092\u8a18\u8ff0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" >\/\/Fabric\u3067\u306ePath\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u751f\u6210\r\nvar path = new fabric.Path('M 0 0 L 200 100 L 170 200 z');\r\npath.set({ left: 120, top: 120 });\r\ncanvas.add(path);\r\n<\/pre>\n<p>\u51fa\u529b\u7d50\u679c<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/12\/fabric10.png\" alt=\"fabric10\" width=\"308\" height=\"308\" class=\"size-full wp-image-3175\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/12\/fabric10.png 308w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/12\/fabric10-150x150.png 150w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/12\/fabric10-100x100.png 100w\" sizes=\"auto, (max-width: 308px) 100vw, 308px\" \/><\/p>\n<p>svg\u8981\u7d20\u3068\u540c\u3058\u6307\u5b9a\u65b9\u6cd5\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u751f\u6210\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3001D3.svg\u30e1\u30bd\u30c3\u30c9\u304b\u3089\u751f\u6210\u3057\u305fpathLength\u30b3\u30de\u30f3\u30c9\u3092\u76f4\u63a5Fabric\u306b\u6e21\u3057\u3066canvas\u306b\u56f3\u3092\u8a18\u8ff0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n\u4e0b\u8a18\u306f\u3001\u7c21\u5358\u3067\u3059\u304cd3.svg\u3067\u751f\u6210\u3057\u305fpathLength\u30b3\u30de\u30f3\u30c9\u3092Fabric\u306b\u6e21\u3057\u3066\u8a18\u8ff0\u3057\u305f\u4f8b\u3067\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" >var w = 600;\r\nvar h = 600;\r\nvar margin = 200;    \r\nvar canvas = new fabric.Canvas('canvas');\r\n\r\n\r\nvar svg = d3.select(&quot;svg&quot;);\r\n\r\nvar data = [\r\n        {x:10, y:20},\r\n        {x:30, y:10},\r\n        {x:48, y:60},\r\n        {x:89, y:5},\r\n        {x:120, y:100},\r\n        {x:160, y:40},\r\n        {x:200, y:83}\r\n    ];\r\n\r\nvar xMax = d3.max(data, function(d){ return d.x } );    \r\nvar yMax = d3.max(data, function(d){ return d.y } );\r\n\r\nvar xScale = d3.scale.linear().domain([0, xMax]).range([0, w-margin]);\r\nvar yScale = d3.scale.linear().domain([0, yMax]).range([0, h-margin]);\r\n\r\n\/\/\u30c7\u30fc\u30bf\u304b\u3089pathLength\u30b3\u30de\u30f3\u30c9\u751f\u6210\u3059\u308b\u95a2\u6570\u3000(D3)    \r\nvar line = d3.svg.line()\r\n    .x(function(d){ return xScale(d.x) })\r\n    .y(function(d){ return yScale(d.y) })\r\n    .interpolate(&quot;cardinal&quot;)\r\n\r\n\/\/pathLength\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u7528\u3057\u3066canvas\u4e0a\u306b\u63cf\u753b\u3000(Fabric)   \r\nvar path = new fabric.Path(line(data));\r\n    path.set({ fill: 'none', stroke: 'red', opacity: 1, &quot;stroke-width&quot;: 4});\r\n    canvas.add(path);    <\/pre>\n<p><a href=\"http:\/\/shimz.me\/example\/d3js\/fabric\/03\/\">example<\/a><\/p>\n<p>\u3055\u3089\u306b\u9762\u767d\u3044\u3053\u3068\u306bFabric\u3067\u306f\u3001canvas\u306b\u8ffd\u52a0\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u3059\u3079\u3066\u30a4\u30f3\u30bf\u30e9\u30af\u30c6\u30a3\u30d6\u306b\u64cd\u4f5c(\u62e1\u5927\u7e2e\u5c0f\u3084\u56de\u8ee2)\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><a href=\"http:\/\/shimz.me\/example\/d3js\/fabric\/03\/\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/12\/d3fabric.jpg\" alt=\"d3fabric\" width=\"400\" height=\"399\" class=\"size-full wp-image-3176\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/12\/d3fabric.jpg 400w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/12\/d3fabric-150x150.jpg 150w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/12\/d3fabric-100x100.jpg 100w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><\/p>\n<p>\u3053\u306e\u3088\u3046\u306b\u3001Fabric.js\u306fD3.js\u3068\u975e\u5e38\u306b\u76f8\u6027\u306e\u3088\u3044\u30b0\u30e9\u30d5\u30a3\u30c3\u30af\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u306e\u3067\u3001\u8208\u5473\u3042\u308b\u65b9\u306fD3\u3068\u4e00\u7dd2\u306b\u89e6\u3063\u3066\u307f\u3066\u306f\u3044\u304b\u304c\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n<p><a href=\"https:\/\/gunmagisgeek.com\/blog\/category\/d3-js\">\u305d\u306e\u4ed6\u306eD3.js\u306e\u8a18\u4e8b\u306f\u3053\u3061\u3089<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>d3.js Advent Calendar 201&hellip;<\/p>\n","protected":false},"author":1,"featured_media":3177,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-3174","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\/3174","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=3174"}],"version-history":[{"count":1,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/3174\/revisions"}],"predecessor-version":[{"id":3190,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/3174\/revisions\/3190"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media\/3177"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=3174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=3174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=3174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}