{"id":117,"date":"2014-03-09T21:46:45","date_gmt":"2014-03-09T13:46:45","guid":{"rendered":"http:\/\/www.hightopo.com\/blog\/?p=117"},"modified":"2014-03-09T21:48:37","modified_gmt":"2014-03-09T13:48:37","slug":"100%e8%a1%8cjs%e5%ae%9e%e7%8e%b0html5%e7%9a%843d%e8%b4%aa%e5%90%83%e8%9b%87%e6%b8%b8%e6%88%8f","status":"publish","type":"post","link":"https:\/\/www.hightopo.com\/blog\/117.html","title":{"rendered":"100\u884cJS\u5b9e\u73b0HTML5\u76843D\u8d2a\u5403\u86c7\u6e38\u620f"},"content":{"rendered":"<p><a href=\"http:\/\/js1k.com\/\">js1k.com<\/a>\u6536\u96c6\u4e86\u5c0f\u4e8e1k\u7684javascript\u5c0f\u4f8b\u5b50\uff0c\u91cc\u9762\u6709\u5f88\u591a\u5f88\u70ab\u5f88\u9177\u7684\u6e38\u620f\u548c\u7279\u6548\uff0c<a href=\"http:\/\/js1k.com\/2014-dragons\/rules\">\u4eca\u5e74\u89c4\u5219<\/a>\u53c8\u589e\u52a0\u4e86\u65b0\u82b1\u6837\uff0c\u4f20\u7edf\u7684classic\u7c7b\u578b\u57fa\u7840\u4e0a\u53c8\u589e\u52a0\u4e86WebGL\u7c7b\u578b\uff0c\u4ee5\u53ca\u5141\u8bb8\u589e\u52a0\u52302K\u7684++\u7c7b\u578b\uff0c\u591a\u6b21\u60f3\u5c1d\u8bd5\u63d0\u4ea4\u4e2a\u5c0f\u6e38\u620f\u4f46\u603b\u65e0\u6cd5\u5199\u51fa\u8ba9\u81ea\u5df1\u6ee1\u610f\u8fd8\u80fd\u63a7\u5236\u5728\u8fd9\u4e48\u5c0f\u7684\u5b57\u8282\u8303\u56f4\u3002<\/p>\n<p>\u81ea\u5df1\u5199\u4e0d\u51fa\u6765\uff0c\u7ad9\u5728\u5de8\u4eba\u80a9\u8180\u603b\u662f\u6709\u673a\u4f1a\u5427\uff0c\u6298\u817e\u4e86\u4e00\u756a\u7ad9\u5728<a href=\"http:\/\/www.hightopo.com\/\">Hightopo<\/a>\u7684\u80a9\u8180\u4e0a\u641e\u4e86\u4e2a3D\u8d2a\u5403\u86c7\u6e38\u620f\uff0c\u7b97\u4e86\u7b97JS\u4ee3\u7801\u8fd8\u53ea\u670990\u6765\u884c\uff0c\u7ec8\u4e8e\u6ee1\u8db3\u4e86\u81ea\u5df1\u7684\u5c0f\u5c0f\u5fc3\u613f\u5199\u5b8c\u8fd9\u7bc7\u53ef\u4ee5\u6ee1\u610f\u53bb\u7761\u89c9\u4e86\u3002<\/p>\n<p><a href=\"http:\/\/www.hightopo.com\/blog\/wp-content\/uploads\/2014\/03\/060128389406803.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-118\" alt=\"060128389406803\" src=\"http:\/\/www.hightopo.com\/blog\/wp-content\/uploads\/2014\/03\/060128389406803.png\" width=\"830\" height=\"589\" srcset=\"https:\/\/www.hightopo.com\/blog\/wp-content\/uploads\/2014\/03\/060128389406803.png 830w, https:\/\/www.hightopo.com\/blog\/wp-content\/uploads\/2014\/03\/060128389406803-300x212.png 300w, https:\/\/www.hightopo.com\/blog\/wp-content\/uploads\/2014\/03\/060128389406803-200x141.png 200w\" sizes=\"(max-width: 830px) 100vw, 830px\" \/><\/a><\/p>\n<p>\u4ee5\u4e0b\u5148\u4e0a\u4e00\u6bb5\u6700\u7ec83D\u6e38\u620f\u5728\u5e73\u677f\u4e0a\u7684\u8fd0\u884c\u4ea4\u4e92<a href=\"http:\/\/v.youku.com\/v_show\/id_XNjgxMzIxOTcy.html\">\u89c6\u9891\u6548\u679c<\/a>\uff1a<br \/>\n<iframe loading=\"lazy\" src=\"http:\/\/player.youku.com\/embed\/XNjgxMzIxOTcy\" height=\"498\" width=\"510\" allowfullscreen=\"\" frameborder=\"0\"><\/iframe><br \/>\n\u4f20\u7edf2D\u7684\u8d2a\u5403\u86c7\u6e38\u620f\u4e00\u822c\u901a\u8fc7\u65b9\u5411\u952e\u76d8\u63a7\u5236\u86c7\u7684\u524d\u8fdb\u65b9\u5411\uff0c\u6211\u4e00\u5f00\u59cb\u5c31\u60f3\u5b9a\u4f4d\u53ef\u8fd0\u884c\u5728\u5e73\u677f\u4e0a\u7684Touch\u4ea4\u4e92\uff0c\u6240\u4ee5\u4e0d\u8003\u8651\u952e\u76d8\u7684\u64cd\u4f5c\u4ea4\u4e92\u65b9\u5f0f\uff0c\u91c7\u7528\u5b8c\u5168\u7528\u70b9\u51fb\u7684\u65b9\u5f0f\u6765\u63a7\u5236\uff0c\u901a\u8fc7<a href=\"http:\/\/www.hightopo.com\/\">HT<\/a>\u7684g3d.getHitPosition(e)\u51fd\u6570\u6211\u80fd\u5f97\u5230\u9f20\u6807\u70b9\u51fb\u6240\u5728\u7684\u5e73\u9762\u4f4d\u7f6e\uff0c\u8fd9\u6837\u4e0e\u86c7\u5934\u7684\u4f4d\u7f6e\u505a\u6bd4\u8f83\u5c31\u80fd\u5224\u65ad\u51fa\u65b0\u7684\u524d\u8fdb\u65b9\u5411\uff0c\u5982\u679c\u70b9\u51fb\u4f4d\u7f6e\u8d85\u51fa\u4e86\u8d2a\u5403\u86c7\u7684\u8fd0\u884c\u77e9\u9635\u8303\u56f4\u6211\u5c31\u4e0d\u505a\u5904\u7406\uff0c\u8fd9\u65f6\u5019\u7559\u7ed9<a href=\"http:\/\/www.hightopo.com\/\">HT<\/a>\u7684\u6807\u51c6orbit\u65cb\u8f6c\u64cd\u4f5c\u65b9\u5f0f\uff0c\u901a\u8fc7ht.Default.isDoubleClick(e)\u76d1\u542c\u53cc\u51fb\u4e8b\u4ef6\u91cd\u542f\u6e38\u620f\u3002\u6240\u8c13\u7684\u53ef\u79fb\u52a8\u5316\u65b9\u9762\u4e5f\u6ca1\u592a\u591a\u9700\u8981\u8003\u8651\u7684\u8bbe\u8ba1\uff0c\u4ec5\u5728\u6dfb\u52a0\u70b9\u51fb\u65f6\u9700\u8981\u8003\u8651touch\u7684\u60c5\u51b5\u00a0view.addEventListener(ht.Default.isTouchable ? &#8216;touchstart&#8217; : &#8216;mousedown&#8217;\u7684\u5904\u7406\u3002<\/p>\n<p>90\u6765\u884c\u6240\u6709JS\u6e90\u4ee3\u7801\u5982\u4e0b\uff0c\u5404\u4f4d\u6e38\u620f\u9ad8\u624b\u4e0d\u8981\u55b7\u6211\uff0c\u80af\u5b9a\u5f88\u591a\u4eba\u53ef\u4ee5\u5199\u5f97\u66f4\u7cbe\u70bc\uff0c\u4f46\u6211\u53ea\u60f3\u901a\u8fc7\u8fd9\u4e2a\u73a9\u4e00\u73a93D\uff0cHTML5\u548cWebGL\uff0c\u4f53\u9a8c\u4e0b\u4f20\u8bf4\u4e2d<a href=\"http:\/\/www.hightopo.com\/\">hightopo<\/a>\u7684<a href=\"http:\/\/www.hightopo.com\/\">HT for Web<\/a>\u7684\u5feb\u901f\u5f00\u53d1\u5230\u5e95\u6709\u591a\u5feb\uff0c\u540c\u65f6\u7ed9\u6574\u5929\u641e\u4f01\u4e1a\u5e94\u7528\u7684\u81ea\u5df1\u6362\u6362\u8111\u5b50\u601d\u8003\u4e9b\u65b0\u5143\u7d20\u3002<\/p>\n<pre class=\"brush:javascript\">function init() {                \r\n\tw = 40; m = 20; d = w * m \/ 2; food = null;            \r\n\tdm = new ht.DataModel();            \r\n\tg3d = new ht.graph3d.Graph3dView(dm);                \r\n\tg3d.setGridVisible(true);\r\n\tg3d.setGridColor('#29B098');\r\n\tg3d.setGridSize(m);\r\n\tg3d.setGridGap(w);            \r\n\tview = g3d.getView();            \r\n\tview.className = 'main';\r\n\tdocument.body.appendChild(view);    \r\n\twindow.addEventListener('resize', function (e) {  g3d.invalidate(); }, false);                                                                                            \r\n\tg3d.sm().setSelectionMode('none');\r\n\tview.addEventListener(ht.Default.isTouchable ? 'touchstart' : 'mousedown', function(e){                \r\n\t\tif(isRunning){\r\n\t\t\tvar p = g3d.getHitPosition(e);\r\n\t\t\tif(Math.abs(p[0]) &lt; d &amp;&amp; Math.abs(p[2]) &lt; d){ \t\t\t\t\tif(direction === 'up' || direction === 'down'){ \t\t\t\t\t\tdirection = p[0] &gt; snake[0].p3()[0] ? 'right' : 'left';                       \r\n\t\t\t\t}\r\n\t\t\t\telse if(direction === 'left' || direction === 'right'){\r\n\t\t\t\t\tdirection = p[2] &gt; snake[0].p3()[2] ? 'down' : 'up';                                             \r\n\t\t\t\t}                        \r\n\t\t\t}\r\n\t\t}else if(ht.Default.isDoubleClick(e)){\r\n\t\t\tstart();    \r\n\t\t}                \r\n\t}, false);                        \r\n\tstart();            \r\n\tsetInterval(function(){ if(isRunning){ isRunning = next(); } }, 200);\r\n}                \r\nfunction start(){\r\n\tdm.clear(); snake = []; score = 0; direction = 'up'; isRunning = true;\r\n\tshape = new ht.Shape();\r\n\tshape.setPoints(new ht.List([\r\n\t\t{x: -d, y: d},\r\n\t\t{x: d, y: d},\r\n\t\t{x: d, y: -d},\r\n\t\t{x: -d, y: -d},\r\n\t\t{x: -d, y: d}\r\n\t]));\r\n\tshape.setThickness(4);\r\n\tshape.setTall(w);\r\n\tshape.setElevation(w\/2);\r\n\tshape.s({'all.color': 'rgba(20, 120, 120, 0.5)', 'all.transparent': true, 'all.reverse.cull': true});\r\n\tdm.add(shape);                         \r\n\tfor(var i=0; i&lt;m\/2; i++) { snake.push(createNode(m\/2 + i, m\/2)); }            \r\n\tcreateFood();                        \r\n}        \r\nfunction createNode(x, y){\r\n\tvar node = new ht.Node();\r\n\tnode.a({ x: x,  y: y });\r\n\tnode.s3(w*0.9, w*0.9, w*0.9);\r\n\tnode.p3(-w*m\/2+w*x+w\/2, w\/2, -w*m\/2+w*y+w\/2);\r\n\tdm.add(node);\r\n\treturn node;\r\n}        \r\nfunction getRandom(){\r\n\treturn parseInt(Math.random() * m);\r\n}        \r\nfunction createFood(){\r\n\tvar x = getRandom(), y = getRandom();\r\n\twhile(touchFood(x, y) || touchSnake(x, y)){ x = getRandom(); y = getRandom(); }\r\n\tif(food) dm.remove(food);            \r\n\tfood = createNode(x, y); \r\n\tfood.s({'shape3d': 'sphere',  'shape3d.color': 'red'});\r\n}        \r\nfunction touchSnake(x, y){\r\n\tfor(var i=0; i&lt;snake.length; i++){                \r\n\t\tif(snake[i].a('x') === x &amp;&amp; snake[i].a('y') === y){ return true; }\r\n\t}\r\n\treturn false;\r\n}        \r\nfunction touchFood(x, y){\r\n\treturn food &amp;&amp; food.a('x') === x &amp;&amp; food.a('y') === y;\r\n}        \r\nfunction next(){\r\n\tvar node = snake[0], x = node.a('x'), y = node.a('y');\r\n\tif(direction === 'up') y--;\r\n\tif(direction === 'down') y++;       \r\n\tif(direction === 'left') x--;\r\n\tif(direction === 'right') x++;\r\n\tif(x &lt; 0 || x &gt;= m || y &lt; 0 || y &gt;= m || touchSnake(x, y)){ return false; }                        \r\n\tif(touchFood(x, y)){\r\n\t\tscore++;                \r\n\t\tsnake.splice(0, 0, createNode(x, y));                \r\n\t\tcreateFood();\r\n\t}else{\r\n\t\tsnake.splice(0, 0, createNode(x, y));\r\n\t\tdm.remove(snake.pop());                \r\n\t}\r\n\treturn true;\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p><a href=\"http:\/\/js1k.com\/\">js1k.com<\/a>\u6536\u96c6\u4e86\u5c0f\u4e8e1k\u7684javascript\u5c0f\u4f8b\u5b50\uff0c\u91cc\u9762\u6709\u5f88\u591a\u5f88\u70ab\u5f88\u9177\u7684\u6e38\u620f\u548c\u7279\u6548\uff0c<a href=\"http:\/\/js1k.com\/2014-dragons\/rules\">\u4eca\u5e74\u89c4\u5219<\/a>\u53c8\u589e\u52a0\u4e86\u65b0\u82b1\u6837\uff0c\u4f20\u7edf\u7684classic\u7c7b\u578b\u57fa\u7840\u4e0a\u53c8\u589e\u52a0\u4e86WebGL\u7c7b\u578b\uff0c\u4ee5\u53ca\u5141\u8bb8\u589e\u52a0\u52302K\u7684++\u7c7b\u578b\uff0c\u591a\u6b21\u60f3\u5c1d\u8bd5\u63d0\u4ea4\u4e2a\u5c0f\u6e38\u620f\u4f46\u603b\u65e0\u6cd5\u5199\u51fa\u8ba9\u81ea\u5df1\u6ee1\u610f\u8fd8\u80fd\u63a7\u5236\u5728\u8fd9\u4e48\u5c0f\u7684\u5b57\u8282\u8303\u56f4\u3002<\/p>\n<p>\u81ea\u5df1\u5199\u4e0d\u51fa\u6765\uff0c\u7ad9\u5728\u5de8\u4eba\u80a9\u8180\u603b\u662f\u6709\u673a\u4f1a\u5427\uff0c\u6298\u817e\u4e86\u4e00\u756a\u7ad9\u5728<a href=\"http:\/\/www.hightopo.com\/\">Hightopo<\/a>\u7684\u80a9\u8180\u4e0a\u641e\u4e86\u4e2a3D\u8d2a\u5403\u86c7\u6e38\u620f\uff0c\u7b97\u4e86\u7b97JS\u4ee3\u7801\u8fd8\u53ea\u670990\u6765\u884c\uff0c\u7ec8\u4e8e\u6ee1\u8db3\u4e86\u81ea\u5df1\u7684\u5c0f\u5c0f\u5fc3\u613f\u5199\u5b8c\u8fd9\u7bc7\u53ef\u4ee5\u6ee1\u610f\u53bb\u7761\u89c9\u4e86\u3002<\/p>\n<p><a href=\"http:\/\/www.hightopo.com\/blog\/wp-content\/uploads\/2014\/03\/060128389406803.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-118\" alt=\"060128389406803\" src=\"http:\/\/www.hightopo.com\/blog\/wp-content\/uploads\/2014\/03\/060128389406803.png\" width=\"830\" height=\"589\" srcset=\"https:\/\/www.hightopo.com\/blog\/wp-content\/uploads\/2014\/03\/060128389406803.png 830w, https:\/\/www.hightopo.com\/blog\/wp-content\/uploads\/2014\/03\/060128389406803-300x212.png 300w, https:\/\/www.hightopo.com\/blog\/wp-content\/uploads\/2014\/03\/060128389406803-200x141.png 200w\" sizes=\"(max-width: 830px) 100vw, 830px\" \/><\/a><\/p>\n<p>\u4ee5\u4e0b\u5148\u4e0a\u4e00\u6bb5\u6700\u7ec83D\u6e38\u620f\u5728\u5e73\u677f\u4e0a\u7684\u8fd0\u884c\u4ea4\u4e92<a href=\"http:\/\/v.youku.com\/v_show\/id_XNjgxMzIxOTcy.html\">\u89c6\u9891\u6548\u679c<\/a>\uff1a<br \/>\n<iframe loading=\"lazy\" src=\"http:\/\/player.youku.com\/embed\/XNjgxMzIxOTcy\" height=\"498\" width=\"510\" allowfullscreen=\"\" frameborder=\"0\"><\/iframe><br \/>\n\u4f20\u7edf2D\u7684\u8d2a\u5403\u86c7\u6e38\u620f\u4e00\u822c\u901a\u8fc7\u65b9\u5411\u952e\u76d8\u63a7\u5236\u86c7\u7684\u524d\u8fdb\u65b9\u5411\uff0c\u6211\u4e00\u5f00\u59cb\u5c31\u60f3\u5b9a\u4f4d\u53ef\u8fd0\u884c\u5728\u5e73\u677f\u4e0a\u7684Touch\u4ea4\u4e92\uff0c\u6240\u4ee5\u4e0d\u8003\u8651\u952e\u76d8\u7684\u64cd\u4f5c\u4ea4\u4e92\u65b9\u5f0f\uff0c\u91c7\u7528\u5b8c\u5168\u7528\u70b9\u51fb\u7684\u65b9\u5f0f\u6765\u63a7\u5236\uff0c\u901a\u8fc7<a href=\"http:\/\/www.hightopo.com\/\">HT<\/a>\u7684g3d.getHitPosition(e)\u51fd\u6570\u6211\u80fd\u5f97\u5230\u9f20\u6807\u70b9\u51fb\u6240\u5728\u7684\u5e73\u9762\u4f4d\u7f6e\uff0c\u8fd9\u6837\u4e0e\u86c7\u5934\u7684\u4f4d\u7f6e\u505a\u6bd4\u8f83\u5c31\u80fd\u5224\u65ad\u51fa\u65b0\u7684\u524d\u8fdb\u65b9\u5411\uff0c\u5982\u679c\u70b9\u51fb\u4f4d\u7f6e\u8d85\u51fa\u4e86\u8d2a\u5403\u86c7\u7684\u8fd0\u884c\u77e9\u9635\u8303\u56f4\u6211\u5c31\u4e0d\u505a\u5904\u7406\uff0c\u8fd9\u65f6\u5019\u7559\u7ed9<a href=\"http:\/\/www.hightopo.com\/\">HT<\/a>\u7684\u6807\u51c6orbit\u65cb\u8f6c\u64cd\u4f5c\u65b9\u5f0f\uff0c\u901a\u8fc7ht.Default.isDoubleClick(e)\u76d1\u542c\u53cc\u51fb\u4e8b\u4ef6\u91cd\u542f\u6e38\u620f\u3002\u6240\u8c13\u7684\u53ef\u79fb\u52a8\u5316\u65b9\u9762\u4e5f\u6ca1\u592a\u591a\u9700\u8981\u8003\u8651\u7684\u8bbe\u8ba1\uff0c\u4ec5\u5728\u6dfb\u52a0\u70b9\u51fb\u65f6\u9700\u8981\u8003\u8651touch\u7684\u60c5\u51b5\u00a0view.addEventListener(ht.Default.isTouchable ? &#8216;touchstart&#8217; : &#8216;mousedown&#8217;\u7684\u5904\u7406\u3002<\/p>\n<p>90\u6765\u884c\u6240\u6709JS\u6e90\u4ee3\u7801\u5982\u4e0b\uff0c\u5404\u4f4d\u6e38\u620f\u9ad8\u624b\u4e0d\u8981\u55b7\u6211\uff0c\u80af\u5b9a\u5f88\u591a\u4eba\u53ef\u4ee5\u5199\u5f97\u66f4\u7cbe\u70bc\uff0c\u4f46\u6211\u53ea\u60f3\u901a\u8fc7\u8fd9\u4e2a\u73a9\u4e00\u73a93D\uff0cHTML5\u548cWebGL\uff0c\u4f53\u9a8c\u4e0b\u4f20\u8bf4\u4e2d<a href=\"http:\/\/www.hightopo.com\/\">hightopo<\/a>\u7684<a href=\"http:\/\/www.hightopo.com\/\">HT for Web<\/a>\u7684\u5feb\u901f\u5f00\u53d1\u5230\u5e95\u6709\u591a\u5feb\uff0c\u540c\u65f6\u7ed9\u6574\u5929\u641e\u4f01\u4e1a\u5e94\u7528\u7684\u81ea\u5df1\u6362\u6362\u8111\u5b50\u601d\u8003\u4e9b\u65b0\u5143\u7d20\u3002<\/p>\n<pre class=\"brush:javascript\">function init() {                \r\n\tw = 40; m = 20; d = w * m \/ 2; food = null;            \r\n\tdm = new ht.DataModel();            \r\n\tg3d = new ht.graph3d.Graph3dView(dm);                \r\n\tg3d.setGridVisible(true);\r\n\tg3d.setGridColor('#29B098');\r\n\tg3d.setGridSize(m);\r\n\tg3d.setGridGap(w);            \r\n\tview = g3d.getView();            \r\n\tview.className = 'main';\r\n\tdocument.body.appendChild(view);    \r\n\twindow.addEventListener('resize', function (e) {  g3d.invalidate(); }, false);                                                                                            \r\n\tg3d.sm().setSelectionMode('none');\r\n\tview.addEventListener(ht.Default.isTouchable ? 'touchstart' : 'mousedown', function(e){                \r\n\t\tif(isRunning){\r\n\t\t\tvar p = g3d.getHitPosition(e);\r\n\t\t\tif(Math.abs(p[0]) &lt; d &amp;&amp; Math.abs(p[2]) &lt; d){ \t\t\t\t\tif(direction === 'up' || direction === 'down'){ \t\t\t\t\t\tdirection = p[0] &gt; snake[0].p3()[0] ? 'right' : 'left';                       \r\n\t\t\t\t}\r\n\t\t\t\telse if(direction === 'left' || direction === 'right'){\r\n\t\t\t\t\tdirection = p[2] &gt; snake[0].p3()[2] ? 'down' : 'up';                                             \r\n\t\t\t\t}                        \r\n\t\t\t}\r\n\t\t}else if(ht.Default.isDoubleClick(e)){\r\n\t\t\tstart();    \r\n\t\t}                \r\n\t}, false);                        \r\n\tstart();            \r\n\tsetInterval(function(){ if(isRunning){ isRunning = next(); } }, 200);\r\n}                \r\nfunction start(){\r\n\tdm.clear(); snake = []; score = 0; direction = 'up'; isRunning = true;\r\n\tshape = new ht.Shape();\r\n\tshape.setPoints(new ht.List([\r\n\t\t{x: -d, y: d},\r\n\t\t{x: d, y: d},\r\n\t\t{x: d, y: -d},\r\n\t\t{x: -d, y: -d},\r\n\t\t{x: -d, y: d}\r\n\t]));\r\n\tshape.setThickness(4);\r\n\tshape.setTall(w);\r\n\tshape.setElevation(w\/2);\r\n\tshape.s({'all.color': 'rgba(20, 120, 120, 0.5)', 'all.transparent': true, 'all.reverse.cull': true});\r\n\tdm.add(shape);                         \r\n\tfor(var i=0; i&lt;m\/2; i++) { snake.push(createNode(m\/2 + i, m\/2)); }            \r\n\tcreateFood();                        \r\n}        \r\nfunction createNode(x, y){\r\n\tvar node = new ht.Node();\r\n\tnode.a({ x: x,  y: y });\r\n\tnode.s3(w*0.9, w*0.9, w*0.9);\r\n\tnode.p3(-w*m\/2+w*x+w\/2, w\/2, -w*m\/2+w*y+w\/2);\r\n\tdm.add(node);\r\n\treturn node;\r\n}        \r\nfunction getRandom(){\r\n\treturn parseInt(Math.random() * m);\r\n}        \r\nfunction createFood(){\r\n\tvar x = getRandom(), y = getRandom();\r\n\twhile(touchFood(x, y) || touchSnake(x, y)){ x = getRandom(); y = getRandom(); }\r\n\tif(food) dm.remove(food);            \r\n\tfood = createNode(x, y); \r\n\tfood.s({'shape3d': 'sphere',  'shape3d.color': 'red'});\r\n}        \r\nfunction touchSnake(x, y){\r\n\tfor(var i=0; i&lt;snake.length; i++){                \r\n\t\tif(snake[i].a('x') === x &amp;&amp; snake[i].a('y') === y){ return true; }\r\n\t}\r\n\treturn false;\r\n}        \r\nfunction touchFood(x, y){\r\n\treturn food &amp;&amp; food.a('x') === x &amp;&amp; food.a('y') === y;\r\n}        \r\nfunction next(){\r\n\tvar node = snake[0], x = node.a('x'), y = node.a('y');\r\n\tif(direction === 'up') y--;\r\n\tif(direction === 'down') y++;       \r\n\tif(direction === 'left') x--;\r\n\tif(direction === 'right') x++;\r\n\tif(x &lt; 0 || x &gt;= m || y &lt; 0 || y &gt;= m || touchSnake(x, y)){ return false; }                        \r\n\tif(touchFood(x, y)){\r\n\t\tscore++;                \r\n\t\tsnake.splice(0, 0, createNode(x, y));                \r\n\t\tcreateFood();\r\n\t}else{\r\n\t\tsnake.splice(0, 0, createNode(x, y));\r\n\t\tdm.remove(snake.pop());                \r\n\t}\r\n\treturn true;\r\n}<\/pre>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[3,5,4],"_links":{"self":[{"href":"https:\/\/www.hightopo.com\/blog\/wp-json\/wp\/v2\/posts\/117"}],"collection":[{"href":"https:\/\/www.hightopo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hightopo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hightopo.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hightopo.com\/blog\/wp-json\/wp\/v2\/comments?post=117"}],"version-history":[{"count":2,"href":"https:\/\/www.hightopo.com\/blog\/wp-json\/wp\/v2\/posts\/117\/revisions"}],"predecessor-version":[{"id":120,"href":"https:\/\/www.hightopo.com\/blog\/wp-json\/wp\/v2\/posts\/117\/revisions\/120"}],"wp:attachment":[{"href":"https:\/\/www.hightopo.com\/blog\/wp-json\/wp\/v2\/media?parent=117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hightopo.com\/blog\/wp-json\/wp\/v2\/categories?post=117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hightopo.com\/blog\/wp-json\/wp\/v2\/tags?post=117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}