Skip to content

Instantly share code, notes, and snippets.

@ToQoz
Created May 29, 2011 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ToQoz/997755 to your computer and use it in GitHub Desktop.
Save ToQoz/997755 to your computer and use it in GitHub Desktop.
canvas上でproccessing.js使うサンプル
<!DOCTYPE html>
<html>
<head>
<title>Processing</title>
<meta charset="utf-8">
</head>
<body>
<canvas width="500" height="400"></canvas>
<script src="js/processing-1.1.0.min.js"></script>
<script>
window.onload = function(){
var canvas = document.getElementsByTagName('canvas')[0];
var codeElm = document.getElementById('processing-code');
var code = codeElm.textContent || codeElm.innerText;
new Processing(canvas, code);
};
</script>
<script id="processing-code" type="application/processing">
//Processing code
void setup() {
size(320, 320); // 9leaps
frameRate(30);
noCursor(); // clear mouse cursor
rectMode(CENTER);
boss = new Boss(width/2, 30);
}
void ship(int x, int y){
stroke(255, 255, 255);
noFill();
triangle(x, y - 7, x- 10, y + 7, x + 10, y + 7);
if (mousePressed) {
line(x, y - 7, x, 0); //draw beam
}
}
class Tama {
float tx, ty, tr, dx, dy;
Tama(float x, float y, float r, float ldx, float ldy) {
tx = x;
ty = y;
tr = r;
dx = ldx;
dy = ldy;
}
boolean update() {
tx += dx;
ty += dy;
stroke(255, 0, 0);
ellipse(tx, ty, tr, tr);
if (ty > height || ty <0 ||tx >width|| tx<0) {
return false;
}
return true;
}
}
class Boss {
float bx, by;
ArrayList danmaku;
Boss(float x, float y){
bx = x;
by = y;
danmaku = new ArrayList();
}
void fire_360(float x, float y){
for (int i=0; i<360;i +=10) {
float rad = radians(i);
danmaku.add(new Tama(x, y, 10,cos(rad), sin(rad)));
}
}
void update(){
float dx;
dx = 50.0 * sin(radians(frameCount * 4));
stroke(0,255,0);
rect(bx + dx, by, 50, 20);
if(frameCount % 30 == 0){
fire_360(bx + dx, by);
}
for (int i = danmaku.size() - 1; i >=0; i--;){
Tama t = (Tama)danmaku.get(i);
if(t.update() == false){
danmaku.remove(i);
}
}
}
}
void draw() {
background(0);
ship(mouseX, mouseY);
boss.update();
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment