Stars - II
Pricessing stars
by schrodingers
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/processing.js/1.4.15/processing.js"></script>
<canvas></canvas>
CSS
body {
overflow: hidden;
margin: 0;
padding: 0;
}
</style> <script type="text/javascript"> window.addEventListener('load', function() {
var scripts=document.body.getElementsByTagName('script');
var canvases=document.body.getElementsByTagName('canvas');
new Processing(canvases[0], scripts[0].text);
}
, false);
// Here prevent javascript in body from throwing error </script> <style>
JavaScript
/*
title: Stars
date: 2015-07-16
*/
void setup() {
size(800, 600);
smooth();
background(0);
noStroke();
}
void draw() {
fill(0, 0, 0, 10);
rect(0, 0, width, height);
colorMode(HSB, 360, 100, 100);
}
void star(float x, float y, float radius1, float radius2, float npoints) {
float angle = TWO_PI / npoints;
float halfAngle = angle / 2.0;
beginShape();
for (int a = 0; a < TWO_PI; a += angle) {
float sx = x + cos(a) * radius2;
float sy = y + sin(a) * radius2;
vertex(sx, sy);
sx = x + cos(a + halfAngle) * radius1;
sy = y + sin(a + halfAngle) * radius1;
vertex(sx, sy);
}
endShape(CLOSE);
}
void mousePressed() {
if (mouseButton == RIGHT) {
background(255);
}
}
void mouseMoved() {
fill(212, 90, 100);
// float d = dist(mouseX, mouseY, pmouseX, pmouseY);
float offsetX = mouseX + random(-30, 50);
float offsetY = pmouseY + random(-20, 60);
star(offsetX, offsetY, random(8, 15), random(16, 35), 5);
}
void mouseDragged() {
fill(random(mouseX, 320), 90, 100);
// float d = dist(mouseX, mouseY, pmouseX, pmouseY);
// constrain(d, 0, 100);
// float w = map(d, 0, 100, 1, 10);
star(mouseX+random(55), pmouseY+random(44), random(8, 15), random(16, 35), 6);
}
void mouseReleased() {
noStroke();
fill(255, 16);
rect(0, 0, width, height);
}
void mouseClicked() {
fill(255, 0, 0, 128);
float d = random(15, 36);
fill(10*d, 95, 95);
ellipse(mouseX, mouseY, d, d);
}
void star(int x, int y, float radius1, float radius2, int npoints) {
float angle = TWO_PI / npoints;
float halfAngle = angle / 2.0;
beginShape();
for (int a = 0; a < TWO_PI; a += angle) {
float sx = x + cos(a) * radius2;
float sy = y + sin(a) * radius2;
vertex(sx, sy);
sx = x + cos(a + halfAngle) * radius1;
sy = y + sin(a + halfAngle) * radius1;
vertex(sx, sy);
}
...