ã¯ããã«
ä»æ¥ã jQuery ã®ä½è
ã¨ãã¦æå㪠John Resig ããã Processing.js ã¨ãã JavaScript ã®ã©ã¤ãã©ãªãå
¬éãã¾ããã
John Resig - Processing.js
ãã®ã©ã¤ãã©ãªã使ãã¨ãæ¯è¼çç°¡åã«ä»¥ä¸ã®ãããªã°ã©ãã£ãã¯ã¹ãã¢ãã¡ã¼ã·ã§ã³ãæ¸ããã¨ãã§ããããã«ãªãã¾ãã
ã¨ããããã§ãå
¬éãããã°ããã®ãã®ã©ã¤ãã©ãªãç°¡åãªä½¿ãæ¹ãã詳ãã使ãæ¹ã¾ã§ã¨ãã¨ãæãä¸ãã¦ã¿ããã¨æãã¾ãã
Processing.js æ¦è¦
ã¾ãã Processing.js ã¨ã¯ä½ãã¨ãã話ããã¾ãã
Processing.js ã¨ã¯ããã©ã¦ã¶ã§ Processing ã¨ããããã°ã©ãã³ã°è¨èªãå®è¡ãã JavaScript ã®ã©ã¤ãã©ãªã§ãã
ã§ã¯ã Processing ã¨ã¯ã©ã®ãããªããã°ã©ãã³ã°è¨èªãªã®ã§ããããã
Processing ã¨ã¯
Processing ã¨ã¯ããã¸ã¥ã¢ã«ãã¶ã¤ã³ã®ããã®ããã°ã©ãã³ã°è¨èªã§ã Java ã®ãããªææ³ããã¦ãã¾ãã
ãã¸ã¥ã¢ã«ãã¶ã¤ã³ã®ããã®ããã°ã©ãã³ã°è¨èªã¨ããã¨é£ãããã§ãããå®éã¯ã¨ã¦ãç°¡åã§ãã
ãã¨ãã°ã以ä¸ã® 2 è¡ãæ¸ãã°ã丸ãæ¸ãã¦ãã¾ãã¾ãã
size(200, 200); ellipse(100, 100, 100, 100);
ããããããããããæãã¦ã¿ã¾ããã
size(200, 200); background(0); noStroke(); for (int i = 0; i < 100; i ++) { fill(random(255), random(255), 255, 100); ellipse(random(200), random(200), 60, 60); }
ç°¡åã§ãã^^
ã¨ããããã§ããã® Processing ããã©ã¦ã¶ã§ä½¿ããããã«ãªãã®ã§ãããããããã¾ããï¼
Processing.js ã使ãããã®æºå
Processing.js ã使ãããã®æºåããã¾ãããã Processing.js ã使ãã«ã¯ã以ä¸ã®æé ãå¿ è¦ã§ãã
- Processing.js ããã¦ã³ãã¼ãããã
- HTML ãã¡ã¤ã«ãä½ã£ã¦ãProcessing.js ãèªã¿è¾¼ãã
- canvas è¦ç´ ãæ¿å ¥ããã
- Processing ã¨ããé¢æ°ãå¼ã³åºãã
ã¡ãã£ã¨ããã©ããããã§ããããã£ã¦ã¿ãã¨ç°¡åã§ããé çªã«ç´°ãã説æãã¾ãã
Processing.js ããã¦ã³ãã¼ãããã
以ä¸ã® JavaScript ãã¡ã¤ã«ããã¦ã³ãã¼ããã¾ãã
http://ejohn.org/apps/processing.js/processing.js
HTML ãã¡ã¤ã«ãä½ã£ã¦ã Processing.js ãèªã¿è¾¼ã
以ä¸ã®ããã«ãHTML ãã¡ã¤ã«ãä½ã£ã¦ script è¦ç´ ã® src å±æ§ã«ãã£ããã¦ã³ãã¼ããã¦ãã processing.js ã®ãã¡ã¤ã«ãæå®ãã¾ãã
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <head> <title>Processing Sample</title> <script type="text/javascript" src="processing.js"></script> </head> <body> <h1>Processing Sample</h1> <div> </div> </body> <html>
canvas è¦ç´ ãæ¿å ¥ãã
ãã£ãã® HTML ãã¡ã¤ã«ã« canvas è¦ç´ ãæ¿å ¥ãã¾ãã width å±æ§ã¨ height å±æ§ã§ canvas è¦ç´ ã®å¤§ãããæå®ãã¾ãã
ï¼ <body> <h1>Processing Sample</h1> <div> <canvas width="400" height="400"></canvas> </div> </body>
サンプル
ã¡ãªã¿ã«ã canvas è¦ç´ ã¨ã¯ã JavaScript ããçµµãæããè¦ç´ ã§ããã¾ãã IE ã«ã¯ canvas è¦ç´ ã¯ããã¾ãããã excanvas.js ãèªã¿è¾¼ããã¨ã§ canvas è¦ç´ ã使ããããã«ãªãã¾ãã
Processing ã¨ããé¢æ°ãå¼ã³åºã
Processing.js ã«ãã£ã¦ Processing é¢æ°ãä½ããã¦ããã®ã§ã JavaScript ãã Processing é¢æ°ãå¼ã³åºãã¾ãã
å¼æ°ã«ã¯ã
- canvas è¦ç´
- Processing ã®ã½ã¼ã¹ã³ã¼ã
ãæå®ãã¾ãã
ï¼ <title>Processing Sample</title> <script type="text/javascript" src="processing.js"></script> <script type="text/javascript"> // ãã¼ããããæã«å®è¡ window.onload = function() { // canvas è¦ç´ var canvas = document.getElementsByTagName('canvas')[0]; // Processing ã®ã½ã¼ã¹ã³ã¼ã var code = "size(400, 400); noStroke(); fill(255, 0, 0, 255 * 0.5); ellipse(200, 150, 200, 200); fill(0, 255, 0, 255 * 0.5); ellipse(250, 250, 200, 200); fill(0, 0, 155, 255 * 0.5); ellipse(150, 250, 200, 200);"; // Processing é¢æ°ãå¼ã³åºã Processing(canvas, code); }; </script> </head> ï¼
サンプル
ããã§ã丸ãæããã¾ãã
æºåãç°¡åã§ããã
ãã£ã¨æºåï¼å¿ è¦ãªäººã¯ï¼
åºæ¬çãªæºåã¯å®äºã§ããããã 2 åãã¯ããã¯ã追å ãã¦ããã¾ãã
ããã¯ãå¥ã«ãããªãã¦ãããã§ããããã©ããªäººãã¡ãªãããæããªã人ã¯ãã®é
ã¯é£ã°ãã¦ãã ãã^^ï¼
- script è¦ç´ ã« Processing ã®ã½ã¼ã¹ã³ã¼ããæ¸ã
- IE ã«ã対å¿ããã
script è¦ç´ ã« Processing ã®ã½ã¼ã¹ã³ã¼ããæ¸ã
Processing ã®ã½ã¼ã¹ã³ã¼ããæååã§æ¸¡ãã®ã¯ããã©ããããã§ããï¼ï¼
ã¨ããããã§ããããªã¨ã㯠script è¦ç´ ã使ãã¾ãããã
script è¦ç´ ã¯ã type å±æ§ã«å®è¡ã§ããªãã¿ã¤ããæå®ãããã¨ä½ããã¾ãããéã«ãããå©ç¨ã㦠script è¦ç´ ã« Processing ã®ã½ã¼ã¹ã³ã¼ããæ¸ãã®ã§ãã
ï¼ <script type="text/javascript" src="processing.js"></script> <script type="text/javascript"> window.onload = function() { var canvas = document.getElementsByTagName('canvas')[0]; // Processing ã®ã½ã¼ã¹ã³ã¼ããæ¸ããã script è¦ç´ var codeElm = document.getElementById('processing-code'); // è¦ç´ ã®å 容ãåå¾ãã var code = codeElm.textContent || codeElm.innerText; Processing(canvas, code); }; </script> <script id="processing-code" type="application/processing"> // ããã« Processing ã®ã½ã¼ã¹ã³ã¼ããæ¸ãã size(400, 400); noStroke(); fill(255, 0, 0, 255 * 0.5); ellipse(200, 150, 200, 200); fill(0, 255, 0, 255 * 0.5); ellipse(250, 250, 200, 200); fill(0, 0, 155, 255 * 0.5); ellipse(150, 250, 200, 200); </script> ï¼
サンプル
ãããã¦ããã°ã Processing ã®ã½ã¼ã¹ã³ã¼ããé·ããªã£ã¦ãå¯èªæ§ãæãªããã«ãã¿ã¾ãã^^
IE ã«ã対å¿ããã
åã®é
ã§ãããã©ã£ã¨è¨ãã¾ããã IE 㯠canvas è¦ç´ ã«å¯¾å¿ãã¦ããªãã®ã§ excanvas.js ã¨ãã JavaScript ã®ãã¡ã¤ã«ãèªã¿è¾¼ãã§ãããå¿
è¦ãããã¾ãã
excanvas.js ã¯ä»¥ä¸ã®ãã¼ã¸ãããã¦ã³ãã¼ãã§ãã¾ãã
http://sourceforge.net/project/showfiles.php?group_id=163391
ãã¦ã³ãã¼ããã excanvas_0002.zip ãã¡ã¤ã«ã解åããã¨ã excanvas.js ãå
¥ã£ã¦ããã®ã§ãããã script ã¿ã°ã§èªã¿è¾¼ã¿ã¾ãã
ï¼ <head> ï¼ <script type="text/javascript" src="excanvas.js"></script> ï¼ </head> ï¼
サンプル
excanvas.js ã«ã¤ãã¦ã¯ã詳ãã解説ãã¾ãããã¨ããããèªã¿è¾¼ãã§ããã°ééãããã¾ããã IE 以å¤ã®ç°å¢ã§ã¯ä½ãããªãç¡å®³ãª JS ãªã®ã§ãå®å¿ã§ãã
ã»ãã IE ã§ãåãã¦ã¾ããï¼
æºåå®äºï¼ä½ãæ¸ãã¦ã¿ããï¼
ã¨ããããã§ãã¡ãã£ã¨ã¢ãã¡ã¼ã·ã§ã³ãæ¸ãã¦ã¿ã¾ãããã³ã¡ã³ããåèã«ãã¦ãã ããã
Particle particles = new Particle[20]; // setup 㯠window.onload ã®ãããªãã® void setup() { size(400, 400); for (int i = 0; i < particles.length; i ++) particles[i] = new Particle(); } // draw ã¯ã¿ã¤ã ã©ã¤ã³ãæ´æ°ãããããã«å¼ã°ãã void draw() { for (int i = 0; i < particles.length; i ++) particles[i].update(); } // ç·ã管çããã¯ã©ã¹ãä½ã class Particle { // ãã£ã¼ã«ãã®å®ç¾© float x, y, xvel, yvel, r, g, b, a; // ã³ã³ã¹ãã©ã¯ã¿ã§ããã£ã¼ã«ãã®åæå Particle () { x = random(width); y = random(height); r = random(255); g = random(255); b = random(255); a = 0; xvel = random(1) * 10 - 5; yvel = random(1) * 10 - 5; } // draw ããå¼ã³åºã // ãã£ã¼ã«ããæ´æ°ãã¦æç»ãã void update() { if (a > 2 * 3.14) return; float px = x, py = y; x += xvel, y += yvel; yvel += 0.1; if (x < 0 || width < x) xvel *= -1; if (y < 0 || height < y) yvel *= -1; stroke(r, g, b, sin(a += 0.01) * 255); line(px, py, x, y); } }
サンプル
ãã¡ãããã¯ãã¹ãã©ã¦ã¶ã§åãã¾ããï¼ãã ã IE ã ã¨éãã§ãï¼ï¼ï¼
ãã£ã¨ Processing.js ã®ãµã³ãã«ãè¦ããããã¯ã以ä¸ã«è¡ãã¨ããã§ãã
http://ejohn.org/apps/processing.js/examples/basic/
Processing ã®ææ³ã¯ã以ä¸ã«ããã¾ãã
Language Reference (API) \ Processing 3+
çããããèªåã§é¢ç½ããã®ãä½ã£ã¦ã¿ã¦ãã ããï¼
ããã§ã¯ãæ¸ããªãã£ãã§ããæ¬ç©ã® Processing ã¨åæ§ã«ãã¦ã¹ã¨ã®é£æºããã¼ãã¼ãã¨ã®é£æºãã§ãã¾ããï¼
JavaScript ã¨ã®å¤ã®å ±æ
Processing.js ã§ã¯ã JavaScript ã®ã°ãã¼ãã«å¤æ°ãã°ãã¼ãã«é¢æ°ã Processing ã®ã°ãã¼ãã«å¤æ°ãã°ãã¼ãã«é¢æ°ã¨ãã¦ä½¿ããã¨ãã§ãã¾ãã
ãã¨ãã°ã以ä¸ã®ããã« JavaScript ã§å®ç¾©ããã°ãã¼ãã«å¤æ° message ã Processing ããå¼ã³åºããã¨ãã§ãã¾ãã
<script type="text/javascript"> window.onload = function() { var canvas = document.getElementsByTagName('canvas')[0]; var codeElm = document.getElementById('processing-code'); var code = codeElm.textContent || codeElm.innerText || codeElm.text; Processing(canvas, code); }; // ãã®é¢æ°ã Processing ããå¼ã³åºã function message(msg) { document.getElementById('message').innerHTML = msg; } </script> <script id="processing-code" type="application/processing"> void setup() { size(400, 400); noStroke(); frameRate(10); } void draw() { fill(0, 0, 0, 100); float r = random(400 / 2), x = random(400), y = random(400); ellipse(x, y, r, r); // ãã㧠JavaScript ã®é¢æ°ãå¼ã¶ message("r = " + r + ", x = " + x + ", y = " + y); } </script>
サンプル
ãããªæãã«ãªãã¾ãã canvas ã®ä¸ã«æååãæ¿å
¥ããã¦ãã¾ããã
ãã®ä»çµã¿ã使ã㨠Processing ã®ã¢ãã¡ã¼ã·ã§ã³ã¨ã HTML ã®é£æºãç°¡åã«è¡ããã¨ãã§ãã¾ãã
Processing.js ã®ä»çµã¿
ããããã¯ã Processing.js ã®å é¨ã®è©±ããã¾ãããã®é ãç¥ããªãã¦ã Processing.js ã¯ä½¿ããã®ã§ãå ¨ç¶é£ã°ãã¦ããã§ã^^ï¼
Processing.js ã®å é¨ãç¥ãæå³
Processing ã®é¢æ°å㯠rect ã¨ã ellipse ã¨ãç´æçãªãã®ãå¤ãã§ãããªã®ã§ã Processing é¢æ°åãã Processing.js ã調ã¹ãã°ããcanvas ã§ã¯ããæ¸ããã ã¼ãã£ã¦ã®ãããåãã£ã¦é¢ç½ãã§ãã
ãã¨ããã¾ã«ç¹å®ã®ãã©ã¦ã¶ã§åããªãã¨ããããã®ã§ãããã調ã¹ãã¨ãã«ã¯å
é¨ã®ä»çµã¿ãç¥ã£ã¦ãããæ¹ãããã§ãã^^
Processing.js ã¯å
¨ä½ã§ 1700 è¡ãããããããã¾ããããªã®ã§å
¨ç¶èªãã¾ãã
Processing.js ã®å®è¡ã®æµã
Processing.js ã§ã¯ã以ä¸ã®æµãã§å¦çãé²ã¿ã¾ãã
- Processing é¢æ°ãå¼ã³åºããã
- å é¨ã§ Processing è¨èªã®ã°ãã¼ãã«ã®åå空éã¨ãªããªãã¸ã§ã¯ã p ãçæãããã
- åå空é p ã«é¢æ°ãã¯ã©ã¹ï¼å®é㯠JavaScript ã®ãããã¿ã¤ãï¼ã追å ãããã
- Processing ã®ã½ã¼ã¹ã³ã¼ããå é¨ã® parse é¢æ°ã§ JavaScript ã®ã½ã¼ã¹ã³ã¼ãã«å¤æãããã
- åå空é p ã with ããããã®ã¹ã³ã¼ãå 㧠JavaScript ã®ã½ã¼ã¹ã³ã¼ãã eval ãããã
- ããã¨ãããä¸æè°ãçµµãæç»ãããã
ã§ã¯ãå®éã«ã½ã¼ã¹ãè¦ã¦ã¿ã¾ãããã
9 - 14 è¡ç®: Processing
以ä¸ã Processing é¢æ°ã®å ¨è²ã§ãããã®é¢æ°ï¼å®è¡ã³ã³ããã¹ãï¼ã§ã¯ã this ã¯ã°ãã¼ãã«ãªãã¸ã§ã¯ãã«ãªã£ã¦ããã®ã§ã Processing ã¯ã°ãã¼ãã«é¢æ°ã«ãªãã¾ãã
this.Processing = function Processing( aElement, aCode ) { var p = buildProcessing( aElement ); p.init( aCode ); return p; };
ã§ã buildProcessing ã Processing ã®ã°ãã¼ãã«åå空é p ãçæããé¢æ°ã§ p.init ã Processing â JavaScript ã®ã½ã¼ã¹ã³ã¼ãå¤æã¨å®è¡ãè¡ãé¢æ°ã§ãã
ããã§æ³¨ç®ãã¹ãã¯ã p ãè¿ãã¦ãã¨ããã§ãããProcessing ã®ã°ãã¼ãã«åå空é p ãè¿ãã¦ããã®ã§ JavaScript 㧠Processing ã®åå空éããã¹ã¦ä½¿ããããã§ãã
Hack ãæ¾é¡ã§ãï¼ãã£ã»ãï¼ã§ãã
32 - 263 è¡ç®: parse
ãã®è¡ã§ã¯ã parse ã¨ãã Processing â JavaScript ã®ã½ã¼ã¹ã³ã¼ãå¤æãè¡ãé¢æ°ãå®ç¾©ããã¦ãã¾ãã
window.parse = function parse( aCode, p ) { // ï¼ç¥ï¼ // ãã®ã³ã¼ãã®ä¸ã§é çªã«ãæååã® replace ã¡ã½ããã«ãã£ã¦ JavaScript ã«å¤æããã¦è¡ã // 縦ã«é·ãã³ã¼ããªã®ã§ãã³ã¡ã³ãã追ã£ã¦ããã°å 容ã¯åãã return aCode; }
第äºå¼æ°ã¯ä½¿ã£ã¦ãªãã§ãããã¶ããæ¶ãå¿ãã§ããã
ãã JavaScript ã®ã¨ã©ã¼ãåºãå ´åã¯ããã®æå¾ã®æå¾ã®è¡ã« console.log ã alert ãªã©ãå·®ãè¾¼ãã¨è¯ãããã§ããã
265 - 1693 è¡ç®: buildProcessing
Processing ã®ã°ãã¼ãã«åå空é p ãä½ã buildProcessing é¢æ°ãå®ç¾©ããã¦ãã¾ãã
function buildProcessing( curElement ){ var p = {}; p.PI = Math.PI; p.TWO_PI = 2 * p.PI; // ï¼ç¥ï¼ p.RGB = 1; p.HSB = 2; // ï¼ç¥ï¼ p.ellipse = function(x, y, width, height) { // ï¼ç¥ï¼ }; // ï¼ç¥ï¼ p.init = function(code) { // ï¼ç¥ï¼ }; // ï¼ç¥ï¼ return p; }
ãããªæãã§ããã ã²ããã p ã«é¢æ°ãããããã£ã追å ãã¾ããã¾ããã§ããã®è¿½å ãããé¢æ°ãå¤æ°ã Processing ã®ã°ãã¼ãã«é¢æ°ãã°ãã¼ãã«å¤æ°ã«ãªãããã§ããã
ãã® p ã«ã¯ãã®ãããæ°ã®é¢æ°ãå¤æ°ãå®ç¾©ããã¾ããã¨ãã«éè¦ãªãã®ãè¦ã¦è¡ãã¾ãããã
1565 - 1690 è¡ç®: p.init
ããã§ã¯ãå®éã«å¤æããã JavaScript ã®ã³ã¼ãã eval ããããã¢ãã¡ã¼ã·ã§ã³ã®ã¿ã¤ãã¼ãèµ°ãããããã¨ããåæåå¦çãã¾ã¨ãã¦ãã p.init ã¨ããé¢æ°ãå®ç¾©ããã¦ãã¾ããããã¯ã Processing é¢æ°ã®ä¸ã§å¼ã°ãã¾ãã
p.init = function init(code){ // ï¼ç¥ï¼ if ( code ) { // ããã®ã³ã¼ã㧠p ã eval ããã parse é¢æ°ã§å¤æãããã³ã¼ãã eval ãããã (function(Processing){with (p){ eval(parse(code, p)); }})(p); } // Processing ã®ã³ã¼ãä¸ã« setup é¢æ°ã宣è¨ããå ´åã¯ãã®é¢æ°ãå®è¡ããã if ( p.setup ) { inSetup = true; p.setup(); } // ï¼ç¥ï¼ // Processing ã®ã³ã¼ãä¸ã« draw é¢æ°ã宣è¨ããå ´å㯠p.loop ãå®è¡ããã // p.loop ã¯å é¨ã§ setInterval ãå¼ã³åºãããããããå®æçã« draw é¢æ°ãå¼ã³åºãããã if ( p.draw ) { if ( !doLoop ) { p.redraw(); } else { p.loop(); } } // ã¤ãã³ãã®åæåã®ã³ã¼ã };
ã¢ãã¡ã¼ã·ã§ã³ãå§ã¾ããªãå ´åããsetup æã®ç¶æ ããããã°ãããå ´åã¯ãã®é¢æ°ãããããã°ããã°ããã§ãã
1039 - 1058 è¡ç®: p.loop
ã¢ãã¡ã¼ã·ã§ã³ã®éå§ãè¡ã p.loop ãè¦ã¦ããã¾ãããã
p.loop = function loop() { if ( loopStarted ) return; var looping = setInterval(function() { try { // ããã curFrameRate ã§æå®ãããééã§å¼ã³åºãã¦ãã ãã // curFrameRate 㯠p.frameRate é¢æ°ã§è¨å®ãããã¨ãã§ããã p.redraw(); } catch(e) { clearInterval( looping ); throw e; } }, 1000 / curFrameRate ); loopStarted = true; }
ãããªæãã§ãã·ã³ãã«ã« redraw ãèªãã§ãã ãã§ããã
1025 - 1037 è¡ç®: p.redraw
p.redraw ãè¦ã¦ããã¾ãããã
p.redraw = function redraw() { if ( hasBackground ) { p.background(); } inDraw = true; p.pushMatrix(); p.draw(); p.popMatrix(); inDraw = false; }
ããã background ã¯æ¯åæ¸ãã¦ããããã§ããããããã¨æã£ããã©ãã©ãã«ã hasBackground ãã©ã°ã建ã¦ãæ¹æ³ãç¡ãã£ãã§ãï¼ï¼ï¼æ¯åãèªå㧠background ãå¼ã°ãªãããã¡ã¿ããã§ãããï¼
p.pushMatrix 㨠p.popMatrix ã§ã¯ãç¾å¨ã®å¡ãã®è²ã¨ãã®æç»ç¶æ
ãä¿åãã¦ãæ»ãã¦ã¾ãã
ä»çµã¿ã¯ãããªæãã§ãããã
ãããã®é¢æ°ã ãæ¼ããã¦ããã°ãã ããããã¨ã¯ãããã°ã§ãã¾ãã
ãã£ã±ã jQuery ã®ä½è
ããã ããã£ã¦ãã½ã¼ã¹ããããã§èªã¿ãããã£ãã§ãï¼
ã¾ã¨ã
ããããã¾ã¨ãã¾ãããã¨ã
ãã®ä»ã® Processing.js ã«é¢ããè¨äº
ã¾ã¨ãã®ã¾ã¨ã
JavaScripter ã®ã¿ãªããã Processing ãåãã¦ã¿ã¾ããã
Processinger ã®ã¿ãªããã JavaScript ãåãã¦ã¿ã¾ããã
ããã¾ã