]> git.mdlowis.com Git - proto/jstoys.git/commitdiff
checkin bezier curve example for race track
authorMichael D. Lowis <mike@mdlowis.com>
Thu, 15 Nov 2018 03:26:17 +0000 (22:26 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Thu, 15 Nov 2018 03:26:17 +0000 (22:26 -0500)
racer.html [new file with mode: 0644]
raycast.html

diff --git a/racer.html b/racer.html
new file mode 100644 (file)
index 0000000..583eb9e
--- /dev/null
@@ -0,0 +1,92 @@
+<!doctype html>
+<html>
+  <head>
+    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
+    <title>Racer</title>
+  </head>
+  <body style='background: #fff; margin: 0; padding: 0; width: 100%; height: 100%;'>
+    <canvas id='display' width='1' height='1' style='width: 100%; height: 100%;' />
+    <script>
+"use strict";
+
+const Keys = { left: 37, right: 39, up: 38, down: 40 };
+const Canvas = display.getContext('2d');
+
+const FrameTimer = ((prev = 1, curr = 1)=>({
+    update: (now) => (prev = curr, curr = now, (curr - prev) / 1000),
+    render: () => {
+        Canvas.fillStyle = "Red";
+        Canvas.font = "normal 12pt Arial";
+        Canvas.fillText(Math.round(1/((curr - prev) / 1000)) + " fps", 10, 26);
+    }
+}))();
+
+const Controls = ((self = {})=>{
+    const onKey = (val) => ((e)=>{
+        self[e.keyCode] = val;
+        e.preventDefault && e.preventDefault();
+        e.stopPropagation && e.stopPropagation();
+    });
+    document.addEventListener('keydown', onKey(true), false);
+    document.addEventListener('keyup', onKey(false), false);
+    return self;
+})();
+
+const Camera = ((self = {})=>{
+    let x = 0, y = 0;
+
+    const resizeView = ()=>{
+        if (self.width != window.innerWidth || self.height != window.innerHeight) {
+            self.width = display.width = window.innerWidth;
+            self.height = display.height = window.innerHeight;
+        }
+    };
+
+    const tracePath = (points)=>{
+        console.log(points);
+        Canvas.beginPath();
+        Canvas.moveTo(points[0][0], points[0][1]);
+        points.map((pt) => Canvas.lineTo(pt[0], pt[1]));
+    };
+
+    self.render = ()=>{
+        resizeView();
+
+        const adjust = 5;
+        if (Controls[Keys.left]) x -= adjust;
+        if (Controls[Keys.right]) x += adjust;
+        if (Controls[Keys.up]) y -= adjust;
+        if (Controls[Keys.down]) y += adjust;
+
+        Canvas.save();
+        Canvas.clearRect(0, 0, self.width, self.height);
+
+        Canvas.beginPath();
+        Canvas.moveTo(self.width/2, 0);
+        Canvas.bezierCurveTo(x, y, x, y, self.width/8, self.height);
+        Canvas.stroke();
+
+        Canvas.beginPath();
+        Canvas.moveTo(self.width/2, 0);
+        Canvas.bezierCurveTo(x, y, x, y, 7 * self.width/8, self.height);
+        Canvas.stroke();
+
+        Canvas.restore();
+    };
+
+    resizeView();
+    return self;
+})();
+
+(() => {
+    const loop = (now) => {
+        const seconds = FrameTimer.update(now);
+        Camera.render();
+        FrameTimer.render();
+        requestAnimationFrame(loop);
+    };
+    requestAnimationFrame(loop);
+})();
+    </script>
+  </body>
+</html>
index 5a2484e1be10856225b3f7e310cdc132605778b3..6330e6283e0a2f8bf40e0ee0ac40ba8f02813bd4 100644 (file)
@@ -21,8 +21,7 @@ const START_POS = { x: 15.3, y: -1.2, direction: Math.PI * 0.3};
 const Keys = { left: 37, right: 39, up: 38, down: 40 };
 const Canvas = display.getContext('2d');
 
-const FrameTimer = (()=>{
-    let prev = 1, curr = 1;
+const FrameTimer = ((prev = 1, curr = 1)=>{
     return {
         update: (now) => (prev = curr, curr = now, (curr - prev) / 1000),
         render: () => {
@@ -33,8 +32,7 @@ const FrameTimer = (()=>{
     };
 })();
 
-const Controls = (()=>{
-    const self = {};
+const Controls = ((self = {})=>{
     const onKey = (val) => ((e)=>{
         self[e.keyCode] = val;
         e.preventDefault && e.preventDefault();
@@ -45,9 +43,7 @@ const Controls = (()=>{
     return self;
 })();
 
-const Player = (()=>{
-    const self = START_POS;
-
+const Player = ((self = START_POS)=>{
     const rotate = (angle)=>(self.direction = (self.direction + angle + TAU) % TAU);
 
     const walk = (distance)=>{
@@ -67,18 +63,14 @@ const Player = (()=>{
     return self;
 })();
 
-const Map = (()=>{
-    let grid = new Uint8Array(MAP_SIZE * MAP_SIZE);
+const Map = ((grid = new Uint8Array(MAP_SIZE ** 2))=>{
     return {
         get: (x, y)=>(grid[Math.floor(y) * MAP_SIZE + Math.floor(x)] || -1),
         randomize: ()=>(grid = grid.map((i) => (Math.random() < 0.3 ? 1 : 0))),
     };
 })();
 
-const Camera = (()=>{
-    const self = {};
-    let columns = [], ceiling = null, floor = null;
-
+const Camera = ((self = {}, columns = [], ceiling = null, floor = null)=>{
     const resizeView = ()=>{
         if (self.width != window.innerWidth || self.height != window.innerHeight) {
             self.width = display.width = Math.floor(window.innerWidth * VIEW_SCALER);
@@ -104,9 +96,9 @@ const Camera = (()=>{
     };
 
     self.render = ()=>{
+        resizeView();
         Canvas.save();
         const data = {};
-        resizeView();
 
         Canvas.fillStyle = ceiling;
         Canvas.fillRect(0, 0, self.width, self.height/2);