Skip to content

Instantly share code, notes, and snippets.

@leoken
Created July 28, 2012 20:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leoken/3194708 to your computer and use it in GitHub Desktop.
Save leoken/3194708 to your computer and use it in GitHub Desktop.
A web page created at CodePen.io.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mechanical Grass &middot; CodePen</title>
<!--
Copyright (c) 2012 Erik James Albaugh, http://codepen.io/leoken
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<style>
body {
margin: 0px;
overflow: hidden;
background: -moz-radial-gradient(center, ellipse cover, rgba(255,197,120,1) 0%, rgba(251,157,35,1) 100%);
background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%,rgba(255,197,120,1)), color-stop(100%,rgba(251,157,35,1)));
background: -webkit-radial-gradient(center, ellipse cover, rgba(255,197,120,1) 0%,rgba(251,157,35,1) 100%);
background: -o-radial-gradient(center, ellipse cover, rgba(255,197,120,1) 0%,rgba(251,157,35,1) 100%);
background: -ms-radial-gradient(center, ellipse cover, rgba(255,197,120,1) 0%,rgba(251,157,35,1) 100%);
background: radial-gradient(ellipse at center, rgba(255,197,120,1) 0%,rgba(251,157,35,1) 100%);
}
</style>
<style>
#codepen-footer, #codepen-footer * {
-webkit-box-sizing: border-box !important;
-moz-box-sizing: border-box !important;
box-sizing: border-box !important;
}
#codepen-footer {
display: block !important;
position: fixed !important;
bottom: 0 !important;
left: 0 !important;
width: 100% !important;
padding: 0 10px !important;
margin: 0 !important;
height: 30px !important;
line-height: 30px !important;
font-size: 12px !important;
color: #eeeeee !important;
background-color: #505050 !important;
text-align: left !important;
background: -webkit-linear-gradient(top, #505050, #383838) !important;
background: -moz-linear-gradient(top, #505050, #383838) !important;
background: -ms-linear-gradient(top, #505050, #383838) !important;
background: -o-linear-gradient(top, #505050, #383838) !important;
border-top: 1px solid black !important;
border-bottom: 1px solid black !important;
border-radius: 0 !important;
border-image: none !important;
box-shadow: inset 0 1px 0 #6e6e6e, 0 2px 2px rgba(0, 0, 0, 0.4) !important;
z-index: 300 !important;
font-family: "Lucida Grande", "Lucida Sans Unicode", Tahoma, sans-serif !important;
letter-spacing: 0 !important;
word-spacing: normal !important;
word-spacing: 0 !important;
-webkit-transform: none !important;
-moz-transform: none !important;
-ms-transform: none !important;
-o-transform: none !important;
transform: none !important;
}
#codepen-footer a {
color: #a7a7a7 !important;
text-decoration: none !important;
text-shadow: none !important;
border: 0 !important;
}
#codepen-footer a:hover {
color: white !important;
}
</style>
<script>
// Kill alerts, confirmations and prompts
// window.alert = function(){}; we're going to allow alerts for now
window.confirm = function(){};
window.prompt = function(){};
window.open = function(){};
window.print = function(){};
</script>
</head>
<body>
<canvas id='canvas'></canvas>
<script src="http://tholman.com/series/flash-forward/ports/draw-worm/02/js/matrix.js"></script>
<script>
(function() {
// Draw Worm
// Originally forked from flash project - http://wonderfl.net/c/9os2
//
//
// Click to clear the canvas.
//
function DrawWorm(){
var canvas;
var context;
var width;
var height;
var mouse = {x: 0, y: window.innerHeight - 20};
//Expose the mouse to demo the app.
this.mouse = mouse;
var interval;
var vms = [];
var MAX_NUM = 100;
var N = 80;
var px = 500;
var py = 500;
this.initialize = function(){
canvas = document.getElementById("canvas");
context = canvas.getContext('2d');
width = window.innerWidth;
height = window.innerHeight;
canvas.width = width;
canvas.height = height;
canvas.addEventListener('mousemove', MouseMove, false);
canvas.addEventListener('click', MouseDown, false);
//Set interval - Bad! - I know!
var interval = setInterval(Draw, 20);
}
var Draw = function(){
var len = vms.length;
var i;
for (i = 0; i < len; i++){
var o = vms[i];
if (o.count < N){
DrawWorm(o);
o.count++;
} else {
len--;
vms.splice(i, 1);
i--;
}
}
Check();
}
//Takes a worm (obj) param
var DrawWorm = function (obj){
if (Math.random() > 0.9){
obj.tmt.rotate(-obj.r * 2);
obj.r *= -1;
}
obj.vmt.prependMatrix(obj.tmt);
var cc1x = -obj.w * obj.vmt.c + obj.vmt.tx;
var cc1y = -obj.w * obj.vmt.d + obj.vmt.ty;
var pp1x = (obj.c1x + cc1x) / 2;
var pp1y = (obj.c1y + cc1y) / 2;
var cc2x = obj.w * obj.vmt.c + obj.vmt.tx;
var cc2y = obj.w * obj.vmt.d + obj.vmt.ty;
var pp2x = (obj.c2x + cc2x) / 2;
var pp2y = (obj.c2y + cc2y) / 2;
context.fillStyle = '#000000';
context.strokeStyle = '#000000';
context.beginPath();
context.moveTo(obj.p1x , obj.p1y);
context.quadraticCurveTo(obj.c1x, obj.c1y, pp1x, pp1y);
context.lineTo(pp2x, pp2y);
context.quadraticCurveTo(obj.c2x, obj.c2y, obj.p2x, obj.p2y);
context.closePath();
context.fill();
obj.c1x = cc1x;
obj.c1y = cc1y;
obj.p1x = pp1x;
obj.p1y = pp1y;
obj.c2x = cc2x;
obj.c2y = cc2y;
obj.p2x = pp2x;
obj.p2y = pp2y;
}
var Check = function(){
var x0 = mouse.x;
var y0 = mouse.y;
var vx = x0 - px;
var vy = y0 - py;
var len = Math.min(Magnitude(vx, vy), 50) + 8;
if (len < 10){
return;
}
var matrix = new Matrix2D();
matrix.rotate(-(Math.atan2(vx, vy)));
matrix.translate(x0, y0);
createWorm(matrix, len);
context.beginPath();
context.strokeStyle = '#000000';
context.moveTo(px, py);
context.lineTo(x0, y0);
context.stroke();
context.closePath();
px = x0;
py = y0;
//More logic here for afterwards?
}
var createWorm = function(mtx, len){
var angle = Math.random() * (Math.PI/6 - Math.PI/64) + Math.PI/64;
if(Math.random() > 0.5){
angle *= -1;
}
var tmt = new Matrix2D();
tmt.scale(0.95, 0.95);
//tmt.rotate(angle);
tmt.translate(12, 0);
var w = 0.5;
var obj = new Worm();
obj.c1x = (-w * mtx.c + mtx.tx);
obj.p1x = (-w * mtx.c + mtx.tx);
obj.c1y = (-w * mtx.d + mtx.ty);
obj.p1y = (-w * mtx.d + mtx.ty);
obj.c2x = (w * mtx.c + mtx.tx);
obj.p2x = (w * mtx.c + mtx.tx);
obj.c2y = (w * mtx.d + mtx.ty);
obj.p2y = (w * mtx.d + mtx.ty);
obj.vmt = mtx;
obj.tmt = tmt;
obj.r = angle;
obj.w = len/20;
obj.count = 0;
vms.push(obj);
if (vms.length > MAX_NUM){
vms.shift();
}
}
//Not sure why they do this kinda thing in flash.
var Worm = function(){
this.c1x = null;
this.c1y = null;
this.c2x = null;
this.c2y = null;
this.p1x = null;
this.p1y = null;
this.p2x = null;
this.p2y = null;
this.w = null;
this.r = null;
this.count = null;
this.vmt = null;
this.tmt = null;
}
//Clear the screen,
var MouseDown = function(e) {
e.preventDefault();
canvas.width = canvas.width;
vms = [];
}
var MouseMove = function(e) {
mouse.x = e.layerX - canvas.offsetLeft;
mouse.y = e.layerY - canvas.offsetTop;
}
//Returns Magnitude
var Magnitude = function(x, y){
return Math.sqrt((x * x) + (y * y));
}
}
var app = new DrawWorm();
app.initialize();
function demoApp() {
app.mouse.x += 20;
if (app.mouse.x >= window.innerWidth) {
window.clearInterval( interval );
}
}
var interval = setInterval( demoApp, 20 );
})();
</script>
<div id="codepen-footer">
<a style="color: #f73535 !important;" href="https://codepen.wufoo.com/forms/m7x3r3/def/field14=" onclick="window.open(this.href, null, 'height=517, width=680, toolbar=0, location=0, status=1, scrollbars=1, resizable=1'); return false">Report Abuse</a>
&nbsp;
<a href="/leoken/pen/yflgs" target="_blank">Edit this Pen</a>
</div>
</body>
</html>
// Draw Worm
// Originally forked from flash project - http://wonderfl.net/c/9os2
//
//
// Click to clear the canvas.
//
function DrawWorm(){
var canvas;
var context;
var width;
var height;
var mouse = {x: 0, y: window.innerHeight - 20};
//Expose the mouse to demo the app.
this.mouse = mouse;
var interval;
var vms = [];
var MAX_NUM = 100;
var N = 80;
var px = 500;
var py = 500;
this.initialize = function(){
canvas = document.getElementById("canvas");
context = canvas.getContext('2d');
width = window.innerWidth;
height = window.innerHeight;
canvas.width = width;
canvas.height = height;
canvas.addEventListener('mousemove', MouseMove, false);
canvas.addEventListener('click', MouseDown, false);
//Set interval - Bad! - I know!
var interval = setInterval(Draw, 20);
}
var Draw = function(){
var len = vms.length;
var i;
for (i = 0; i < len; i++){
var o = vms[i];
if (o.count < N){
DrawWorm(o);
o.count++;
} else {
len--;
vms.splice(i, 1);
i--;
}
}
Check();
}
//Takes a worm (obj) param
var DrawWorm = function (obj){
if (Math.random() > 0.9){
obj.tmt.rotate(-obj.r * 2);
obj.r *= -1;
}
obj.vmt.prependMatrix(obj.tmt);
var cc1x = -obj.w * obj.vmt.c + obj.vmt.tx;
var cc1y = -obj.w * obj.vmt.d + obj.vmt.ty;
var pp1x = (obj.c1x + cc1x) / 2;
var pp1y = (obj.c1y + cc1y) / 2;
var cc2x = obj.w * obj.vmt.c + obj.vmt.tx;
var cc2y = obj.w * obj.vmt.d + obj.vmt.ty;
var pp2x = (obj.c2x + cc2x) / 2;
var pp2y = (obj.c2y + cc2y) / 2;
context.fillStyle = '#000000';
context.strokeStyle = '#000000';
context.beginPath();
context.moveTo(obj.p1x , obj.p1y);
context.quadraticCurveTo(obj.c1x, obj.c1y, pp1x, pp1y);
context.lineTo(pp2x, pp2y);
context.quadraticCurveTo(obj.c2x, obj.c2y, obj.p2x, obj.p2y);
context.closePath();
context.fill();
obj.c1x = cc1x;
obj.c1y = cc1y;
obj.p1x = pp1x;
obj.p1y = pp1y;
obj.c2x = cc2x;
obj.c2y = cc2y;
obj.p2x = pp2x;
obj.p2y = pp2y;
}
var Check = function(){
var x0 = mouse.x;
var y0 = mouse.y;
var vx = x0 - px;
var vy = y0 - py;
var len = Math.min(Magnitude(vx, vy), 50) + 8;
if (len < 10){
return;
}
var matrix = new Matrix2D();
matrix.rotate(-(Math.atan2(vx, vy)));
matrix.translate(x0, y0);
createWorm(matrix, len);
context.beginPath();
context.strokeStyle = '#000000';
context.moveTo(px, py);
context.lineTo(x0, y0);
context.stroke();
context.closePath();
px = x0;
py = y0;
//More logic here for afterwards?
}
var createWorm = function(mtx, len){
var angle = Math.random() * (Math.PI/6 - Math.PI/64) + Math.PI/64;
if(Math.random() > 0.5){
angle *= -1;
}
var tmt = new Matrix2D();
tmt.scale(0.95, 0.95);
//tmt.rotate(angle);
tmt.translate(12, 0);
var w = 0.5;
var obj = new Worm();
obj.c1x = (-w * mtx.c + mtx.tx);
obj.p1x = (-w * mtx.c + mtx.tx);
obj.c1y = (-w * mtx.d + mtx.ty);
obj.p1y = (-w * mtx.d + mtx.ty);
obj.c2x = (w * mtx.c + mtx.tx);
obj.p2x = (w * mtx.c + mtx.tx);
obj.c2y = (w * mtx.d + mtx.ty);
obj.p2y = (w * mtx.d + mtx.ty);
obj.vmt = mtx;
obj.tmt = tmt;
obj.r = angle;
obj.w = len/20;
obj.count = 0;
vms.push(obj);
if (vms.length > MAX_NUM){
vms.shift();
}
}
//Not sure why they do this kinda thing in flash.
var Worm = function(){
this.c1x = null;
this.c1y = null;
this.c2x = null;
this.c2y = null;
this.p1x = null;
this.p1y = null;
this.p2x = null;
this.p2y = null;
this.w = null;
this.r = null;
this.count = null;
this.vmt = null;
this.tmt = null;
}
//Clear the screen,
var MouseDown = function(e) {
e.preventDefault();
canvas.width = canvas.width;
vms = [];
}
var MouseMove = function(e) {
mouse.x = e.layerX - canvas.offsetLeft;
mouse.y = e.layerY - canvas.offsetTop;
}
//Returns Magnitude
var Magnitude = function(x, y){
return Math.sqrt((x * x) + (y * y));
}
}
var app = new DrawWorm();
app.initialize();
function demoApp() {
app.mouse.x += 20;
if (app.mouse.x >= window.innerWidth) {
window.clearInterval( interval );
}
}
var interval = setInterval( demoApp, 20 );
<canvas id='canvas'></canvas>
body {
margin: 0px;
overflow: hidden;
background: -moz-radial-gradient(center, ellipse cover, rgba(255,197,120,1) 0%, rgba(251,157,35,1) 100%);
background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%,rgba(255,197,120,1)), color-stop(100%,rgba(251,157,35,1)));
background: -webkit-radial-gradient(center, ellipse cover, rgba(255,197,120,1) 0%,rgba(251,157,35,1) 100%);
background: -o-radial-gradient(center, ellipse cover, rgba(255,197,120,1) 0%,rgba(251,157,35,1) 100%);
background: -ms-radial-gradient(center, ellipse cover, rgba(255,197,120,1) 0%,rgba(251,157,35,1) 100%);
background: radial-gradient(ellipse at center, rgba(255,197,120,1) 0%,rgba(251,157,35,1) 100%);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment