Parcourir la source

Ensure circle is never black

Johannes Zellner il y a 7 ans
Parent
commit
33f51cc22f
1 fichiers modifiés avec 32 ajouts et 21 suppressions
  1. 32 21
      index.html

+ 32 - 21
index.html

@@ -1,6 +1,5 @@
-<html>
-
-<style>
+<html><head>
+<meta http-equiv="content-type" content="text/html; charset=UTF-8"><style>
 
 body, html, canvas {
     padding: 0;
@@ -13,9 +12,9 @@ body, html, canvas {
 
 </style>
 
-<body>
+</head><body>
 
-    <canvas></canvas>
+    <canvas height="627" width="20"></canvas>
 
 <script>
 
@@ -66,6 +65,7 @@ var colors = [
     '#66ff00',
     '#ffff66',
     '#b4ff00',
+    '#ff5400',
     '#ffff33',
     'white',
     'black'
@@ -81,8 +81,16 @@ function randomFloat(min, max) {
     return (Math.random() * (max - min + 1)) + min;
 }
 
-function randomColor() {
-    return colors[Math.floor(Math.random() * colors.length)];
+function randomColor(blacklist) {
+    if (!blacklist) blacklist = [];
+
+    var color;
+    do {
+        color = colors[Math.floor(Math.random() * colors.length)]
+    }
+    while (blacklist.indexOf(color) > -1);
+
+    return color;
 }
 
 var backgroundFillStyle = 'blue';
@@ -141,17 +149,17 @@ function setupClipping(ctx, x, y, w, h) {
 function resetFeature(f) {
     if (f.type === 'line') {
         f.fillStyle = randomColor();
-        f.speed = randomFloat(0.1, 1);
-        f.h = randomInt(10, 50);
+        f.speed = randomFloat(0.5, 2);
+        f.h = randomInt(20, 80);
         f.y = -f.h;
     } else if (f.type === 'circle') {
         if (f.speed < 0) {
             f.r = 0;
-            f.fillStyle = randomColor();
-            f.speed = randomFloat(0.1, 1);
+            f.fillStyle = randomColor(['black']);
+            f.speed = randomFloat(0.8, 1.5);
         } else {
             f.r -= f.speed;
-            f.speed = -randomFloat(0.1, 1);
+            f.speed = -randomFloat(0.8, 1.5);
         }
         f.y = clipRegion.y + clipRegion.h/2;
         f.x = clipRegion.x + clipRegion.w/2;
@@ -173,12 +181,12 @@ if (localStorage.clipRegion) {
 
 function animateContent() {
     // change background maybe
-    // if (randomInt(0, 10000) < 10) backgroundFillStyle = randomColor();
+    // if (randomInt(0, 1000) < 5) backgroundFillStyle = randomColor();
     backgroundFillStyle = 'black';
 
     features.forEach(function (f) {
         if (f.type === 'line') {
-            if (f.y > canvas.height) {
+            if (f.y > canvas.width) {
                 resetFeature(f);
             } else {
                 f.y += f.speed;
@@ -218,13 +226,16 @@ function redraw() {
 
     ctx.save();
 
+    if (mode !== 'clip_edit') {
+        setupClipping(ctx, clipRegion.x, clipRegion.y, clipRegion.w, clipRegion.h);
+    }
+
+    animateContent();
+    drawContent();
+
     if (mode === 'clip_edit') {
         drawEllipse(ctx, 10, 10, 20, 20, 'red');
         drawEllipse(ctx, clipRegion.x, clipRegion.y, clipRegion.w, clipRegion.h, '#ff0000aa');
-    } else {
-        setupClipping(ctx, clipRegion.x, clipRegion.y, clipRegion.w, clipRegion.h);
-        animateContent();
-        drawContent();
     }
 
     ctx.restore();
@@ -288,7 +299,7 @@ function onMouseMove(e) {
 }
 
 function onKeypress(e) {
-    if (e.key === 'Escape') {
+    if (e.key === 'e') {
         mode = mode === 'clip_edit' ? '' : 'clip_edit';
     } else if (e.key === 'f') {
         toggleFullScreen();
@@ -307,5 +318,5 @@ requestAnimationFrame(redraw);
 
 </script>
 
-</body>
-</html>
+
+</body></html>