{"id":378,"date":"2015-10-23T15:10:54","date_gmt":"2015-10-23T06:10:54","guid":{"rendered":"http:\/\/www.takaya-com.jp\/archives\/?p=378"},"modified":"2015-10-26T10:21:57","modified_gmt":"2015-10-26T01:21:57","slug":"svg_css3_transform_sp4","status":"publish","type":"post","link":"https:\/\/takaya-com.jp\/archives\/2015\/10\/svg_css3_transform_sp4\/","title":{"rendered":"JavaScript\u3067SVG\u306epolygon\u3068rect\u3092\u751f\u6210\u3057\u3001CSS\u306etransform\u3067\u591a\u89d2\u67f1\u306b\u3059\u308b"},"content":{"rendered":"<p><!-- \u672c\u6587 --><br \/>\n<a href=\"http:\/\/www.takaya-com.jp\/archives\/2015\/10\/svg_array_polygon\/\">Circle\u4e0a\u306b\u591a\u89d2\u5f62Polygon\u3092\u63cf\u753b\u3059\u308b<\/a>\u3068\u540c\u7b49\u306e\u624b\u6cd5\u3067\u30d5\u30bf\u306b\u306a\u308b\u30dd\u30ea\u30b4\u30f3\u3092\u751f\u6210\u3057\u3001\u3053\u306e\u5186\u3092\u57fa\u6e96\u306b\u5074\u9762rect\u306e\u4f4d\u7f6e\u5408\u308f\u305b\u306b\u5229\u7528\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u89d2\u6570\u3092\u6307\u5b9a\u3059\u308b\u3060\u3051\u30673\u89d2\u67f1\uff5e\u5186\u67f1\uff08\u305f\u3060\u3057\u3068\u3066\u3082\u51e6\u7406\u304c\u91cd\u305f\u304f\u306a\u308b\uff09\u307e\u3067\u751f\u6210\u3067\u304d\u307e\u3059\u3002<br \/>\npolygon\u3001rect\u306e\u8272\u3082\u6307\u5b9a\u3067\u304d\u307e\u3059\u3002\n<\/p>\n<p><!--more--><\/p>\n<p>\u3053\u306e\u30c7\u30e2\u306fChrome46\u3067\u52d5\u4f5c\u78ba\u8a8d\u3092\u3057\u3066\u3044\u307e\u3059\uff08IE\u3067\u306f\u52d5\u4f5c\u3057\u307e\u305b\u3093\uff09<\/p>\n<p>\n\u3053\u306e\u30b3\u30fc\u30c9\u3067\u30011\u30da\u30fc\u30b8\u4e2d\u306b\u8907\u6570\u306e\u591a\u89d2\u67f1\u3092\u8868\u793a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n\u63cf\u753b\u5148\u306ediv\u306bcss\u306etransform\u3092\u304b\u3051\u308b\u3053\u3068\u3067\u89d2\u67f1\u3092\u56de\u8ee2\u3055\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3002<br \/>\n\uff08\u4eca\u56de\u306f\u7acb\u4f53\u3068\u308f\u304b\u308a\u3084\u3059\u3044\u3088\u3046\u306b\u659c\u3081\u4e0a\u304b\u3089\u307f\u3066\u3044\u308b\u3088\u3046\u306bCSS\u3067rotate\u3057\u3066\u3044\u307e\u3059\uff09\n<\/p>\n<p>\u2193\u4ee5\u4e0b\u304c\u5b9f\u969b\u306e\u30bd\u30fc\u30b9\u30b5\u30f3\u30d7\u30eb\u3067\u3059\u3002<\/p>\n<p>\u25bcJavaScript<\/p>\n<pre class=\"lang:js decode:true \" title=\"JavaScript\" >\r\n<script>\r\n\/\/\tdrawPrism(\"\u63cf\u753b\u3059\u308bdiv\u540d\",\u89d2\u5f62\uff08\u6570\u5024\uff09,\u5074\u9762\u306e\u9ad8\u3055\u306e\u534a\u5206_\uff08\u6570\u5024\u307e\u305f\u306f\"eq\"\uff09,\r\n\t\/\/\t\"\u4e0a\u4e0b\u306epolygon\u306e\u8272\",\"\u5074\u9762\u306erect\u306e\u8272\",\u67a0\u7dda\u306e\u8272\",\u67a0\u7dda\u306e\u592a\u3055);\r\n\tdrawPrism(\"box_1\",3,20,\"red\",\"pink\",\"black\",2);\r\n\tdrawPrism(\"box_2\",4,\"eq\",\"#0ff\",\"#00f\",\"#009\",3);\r\n\tdrawPrism(\"box_3\",50,160,\"none\",\"rgba(0,0,0,0.5)\");\r\n\tdrawPrism(\"box_4\",7,100,\"rgba(0,255,0,0.7)\",\"rgba(0,0,0,0.3)\",\"rgba(0,0,0,0.1)\",5);\r\n\r\n\r\nfunction drawPrism(root,pg_num,rect_h,polygon_c,rect_c,stroke_c,stroke_w){\r\n\tvar root_div = document.getElementById(root);\r\n\tvar pg_x = new Array();\r\n\tvar pg_y = new Array();\r\n\tvar pg_points = new String();\r\n\tvar rects_val = new Object;\r\n\tvar rects_name = new Array();\r\n\tvar pgc_x = 0;\r\n\tvar pgc_y = 0;\r\n\t\/\/\tcoc_r:\u30dd\u30ea\u30b4\u30f3\u3092\u63cf\u753b\u3059\u308b\u969b\u306e\u5186\u306e\u534a\u5f84\r\n\tvar coc_r = 100;\r\n\t\/\/\t\u4e0a\u9762\u30fb\u5e95\u9762polygon\u3092\u4e2d\u5fc3\u304b\u3089\u79fb\u52d5\u3055\u305b\u308b\u91cf\uff08\u5074\u9762rect\u306e\u7e26\u306f\u3053\u306e\u5024\u306e2\u500d\uff09\t\r\n\tvar r_ht = rect_h;\r\n\tvar pg_pt = pg_num;\r\n\tvar angle = 360\/pg_pt;\/\/1\u89d2\u3042\u305f\u308a\u306e\u89d2\u5ea6\r\n\t\r\n\t\/\/\tpg_points:polygon\u751f\u6210\u7528\u306ex,y\u5ea7\u6a19\u306e\u53d6\u5f97\r\n\t\/\/\tpg_x,pg_y:\u518d\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u914d\u5217\u306b\u3082\u4fdd\u5b58\r\n\tfor( i = 0 ; i < pg_pt ; i++ ){\r\n\t\tradian = (angle*i+(180-angle)\/2) * Math.PI \/ 180;\r\n\t\tcos_A = Math.cos(radian);\r\n\t\tsin_A = Math.sin(radian);\r\n\t\tx = cos_A * coc_r + coc_r;\r\n\t\ty = sin_A * coc_r + coc_r;\r\n\t\tpg_x.push(x);\r\n\t\tpg_y.push(y);\r\n\t\tpg_points += pg_x[i]+\",\"+pg_y[i]+\" \";\r\n\t}\r\n\r\n\t\/\/\t\u5074\u9762rect\u751f\u6210\u7528\u306e\u5024\u306e\u53d6\u5f97\r\n\t\/\/\u3000r_wt:\u5074\u9762\u306e\u5e45\u306e1\/2\u306e\u5024.\uff08x0,x1\u306e\u5dee\u306e2\u4e57 + y0,y1\u306e\u5dee\u306e2\u4e57)\u306e\u5e73\u65b9\u6839 = 2\u70b9\u9593\u306e\u8ddd\u96e2\u306e1\/2\r\n\tvar r_wt = Math.sqrt(((Math.pow(pg_x[0]-pg_x[1]),2)+Math.pow((pg_x[0]-pg_x[1]),2)))\/2;\r\n\t\/\/rect\u306e\u7e26\u6a2a\u3092\u540c\u3058\u306b\u3059\u308b\u5834\u5408\u306e\u51e6\u7406.\r\n\tif(r_ht==\"eq\"){r_ht=r_wt;}\r\n\t\/\/\u3000rects_val.r:\uff08\u4e2d\u5fc3\u304b\u3089\u89d2\u307e\u3067\u306e\u9577\u3055coc_r\u306e2\u4e57 - rect\u306e\u5e45\u306e1\/2\u306e2\u4e57\uff09\u306e\u5e73\u65b9\u6839 = rect\u3092\u4e2d\u5fc3\u304b\u3089\u79fb\u52d5\u3055\u305b\u308b\u91cf\r\n\trects_val.r = Math.sqrt(Math.pow(coc_r,2)-Math.pow(r_wt,2));\r\n\t\/\/\trects_val.x,y:\u5186\u306e\u4e2d\u5fc3\u304b\u3089rect\u306e\u7e26\u6a2a\u3092\u5f15\u3044\u305f\u5024\u3002\u3053\u3053\u304b\u3089\u63cf\u304d\u59cb\u3081\u308b\u3053\u3068\u3067rect\u306e\u4e2d\u5fc3\u304ccoc_r\u306b\u304f\u308b\r\n\trects_val.x = coc_r-r_wt;\r\n\trects_val.y = coc_r-r_ht;\r\n\trects_val.w = r_wt *2;\r\n\trects_val.h = r_ht *2;\r\n\r\n\t\/\/\t\u4e0a\u9762\u306e\u30dd\u30ea\u30b4\u30f3\u306e\u751f\u6210\r\n\tmake_polygon(\"pg_top\"+root,pg_points);\r\n\thoge = document.getElementById(\"pg_top\"+root);\r\n\thoge.style.transform = \" translateZ(\"+r_ht+\"px)\";\r\n\r\n\t\/\/\t\u4e0b\u9762\u306e\u30dd\u30ea\u30b4\u30f3\u306e\u751f\u6210.\u5947\u6570\u89d2\u306e\u5834\u5408X\u3092180\u5ea6\u56de\u8ee2\u3055\u305b\u308b\u3068\u9802\u70b9\u304c\u53cd\u5bfe\u5411\u304d\u306b\u306a\u308b\u306e\u3067Z\u3082180\u5ea6\u56de\u8ee2\r\n\tmake_polygon(\"pg_btm\"+root,pg_points);\r\n\thoge = document.getElementById(\"pg_btm\"+root);\r\n\thoge.style.transform = \"rotateX(180deg) rotateZ(180deg) translateZ(\"+r_ht+\"px)\";\r\n\r\n\t\/\/\t\u89d2\u6570\u679a\u306e\u5074\u9762rect\u3092\u751f\u6210\r\n\tfor(i=0 ; i<pg_pt ; i++){\r\n\t\tmake_rect(\"rect_\"+i+\"_\"+root,rects_val);\r\n\t\trects_name.push(document.getElementById(\"rect_\"+i+\"_\"+root));\r\n\t\t\/\/\t\u89d2\u6570\u304c\u5947\u6570\u306e\u6642\u306b\u5e95\u8fba\u3092\u6c34\u5e73\u306b\u3059\u308b\u305f\u3081\u306b\u89d2\u5ea6\u3092\u8abf\u6574\r\n\t\tvar ydeg=0;\r\n\t\tif(pg_pt%2==1){ydeg=angle*i-angle\/2}else{ydeg=angle*i;}\r\n\t\tvar tf =\"rotateX(90deg) rotateY(\"+ydeg+\"deg) translateZ(\"+rects_val.r+\"px)\";\r\n\t\trects_name[i].style.transform = tf;\r\n\t}\r\n\r\n\t\/\/\tpolygon\u751f\u6210\r\n\tfunction make_polygon(svg_idname,pg_points){\r\n\t\tvar SVG = \"http:\/\/www.w3.org\/2000\/svg\";\r\n\t\tvar svg = document.createElementNS(SVG,\"svg\");\r\n\t\tsvg.setAttribute(\"id\",svg_idname);\r\n\t\tsvg.setAttribute(\"viewBox\",\"0 0 200 200\");\r\n\t\tvar pg = document.createElementNS(SVG, \"polygon\");\r\n\t\tpg.setAttribute(\"points\",pg_points);\r\n\t\tpg.setAttribute(\"fill\",polygon_c);\r\n\t\tpg.setAttribute(\"stroke-width\",stroke_w);\r\n\t\tsvg.appendChild(pg);\r\n\t\troot_div.appendChild(svg);\r\n\t}\r\n\r\n\t\/\/\t\u5074\u9762rect\u751f\u6210\r\n\tfunction make_rect(svg_idname,rects){\r\n\t\tvar SVG = \"http:\/\/www.w3.org\/2000\/svg\";\r\n\t\tvar svg = document.createElementNS(SVG,\"svg\");\r\n\t\tsvg.setAttribute(\"id\",svg_idname);\r\n\t\tsvg.setAttribute(\"viewBox\",\"0 0 200 200\");\r\n\t\tvar rt = document.createElementNS(SVG, \"rect\");\r\n\t\trt.setAttribute(\"x\",rects.x);\r\n\t\trt.setAttribute(\"y\",rects.y);\r\n\t\trt.setAttribute(\"height\",rects.h);\r\n\t\trt.setAttribute(\"width\",rects.w);\r\n\t\trt.setAttribute(\"fill\",rect_c);\r\n\t\trt.setAttribute(\"stroke\",stroke_c);\r\n\t\trt.setAttribute(\"stroke-width\",stroke_w);\r\n\t\tsvg.appendChild(rt);\r\n\t\troot_div.appendChild(svg);\r\n\t}\r\n}\r\n<\/script>\r\n<\/pre>\n<p>\u25bcCSS<\/p>\n<pre class=\"lang:css decode:true \" title=\"CSS\" >\r\n<style>\r\ndiv{\r\n\ttransform-style: preserve-3d;\r\n\twidth:250px;\r\n\theight:250px;\r\n\tposition:relative;\r\n\ttransform-origin:100px 100px;\r\n\ttransform:rotateX(-30deg) rotateY(-30deg);\r\n\tpadding:25px;\r\n\tfloat:left;\r\n}\r\nsvg{\r\n\ttransform-style: preserve-3d;\r\n\tposition:absolute;\r\n\twidth:200px;\r\n\theight:200px;\r\n}\r\n<\/style>\r\n<\/pre>\n<p>\u25bcHTML<\/p>\n<pre class=\"lang:xhtml decode:true \" title=\"XHTML\" >\r\n<div id=\"box_1\"><\/div>\r\n<div id=\"box_2\"><\/div>\r\n<div id=\"box_3\"><\/div>\r\n<div id=\"box_4\"><\/div>\r\n<\/pre>\n<p><a href=\"http:\/\/www.takaya-com.jp\/archives\/demo\/svg_css3_transform_sp4\/\" target=\"_blank\">\u30c7\u30e2\u30da\u30fc\u30b8\u306f\u3001\u3053\u3061\u3089<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Circle\u4e0a\u306b\u591a\u89d2\u5f62Polygon\u3092\u63cf\u753b\u3059\u308b\u3068\u540c\u7b49\u306e\u624b\u6cd5\u3067\u30d5\u30bf\u306b\u306a\u308b\u30dd\u30ea\u30b4\u30f3\u3092\u751f\u6210\u3057\u3001\u3053\u306e\u5186\u3092\u57fa\u6e96\u306b\u5074\u9762rect\u306e\u4f4d\u7f6e\u5408\u308f\u305b\u306b\u5229\u7528\u3057\u3066\u3044\u307e\u3059\u3002 \u89d2\u6570\u3092\u6307\u5b9a\u3059\u308b\u3060\u3051\u30673\u89d2\u67f1\uff5e\u5186\u67f1\uff08\u305f\u3060\u3057\u3068\u3066\u3082\u51e6\u7406\u304c\u91cd\u305f\u304f\u306a\u308b\uff09\u307e\u3067\u751f\u6210\u3067 [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,3],"tags":[],"_links":{"self":[{"href":"https:\/\/takaya-com.jp\/archives\/wp-json\/wp\/v2\/posts\/378"}],"collection":[{"href":"https:\/\/takaya-com.jp\/archives\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/takaya-com.jp\/archives\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/takaya-com.jp\/archives\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/takaya-com.jp\/archives\/wp-json\/wp\/v2\/comments?post=378"}],"version-history":[{"count":5,"href":"https:\/\/takaya-com.jp\/archives\/wp-json\/wp\/v2\/posts\/378\/revisions"}],"predecessor-version":[{"id":385,"href":"https:\/\/takaya-com.jp\/archives\/wp-json\/wp\/v2\/posts\/378\/revisions\/385"}],"wp:attachment":[{"href":"https:\/\/takaya-com.jp\/archives\/wp-json\/wp\/v2\/media?parent=378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/takaya-com.jp\/archives\/wp-json\/wp\/v2\/categories?post=378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/takaya-com.jp\/archives\/wp-json\/wp\/v2\/tags?post=378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}