WavePlayz

Untitled

Apr 28th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function spline (points, delta, isLooped = true) {
  2.                 let p0, p1, p2, p3
  3.    
  4.                 if (! isLooped) {
  5.                     p0 = Math.floor(delta)
  6.                     p1 = p0 + 1
  7.                     p2 = p1 + 1
  8.                     p3 = p2 + 1
  9.                 } else {
  10.                     p1 = Math.floor( delta )
  11.                     p2 = (p1 + 1) % points.length
  12.                     p3 = (p2 + 1) % points.length
  13.                     p0 = p1 >= 1 ? p1 - 1 : points.length - 1
  14.                 }
  15.    
  16.                 delta = delta - Math.floor( delta )
  17.    
  18.                 let delta_X2 = delta ** 2
  19.                 let delta_X3 = delta_X2 * delta
  20.    
  21.                 let q1 = -delta_X3 + 2*delta_X2 - delta
  22.                 let q2 = 3*delta_X3 - 5*delta_X2 + 2
  23.                 let q3 = -3*delta_X3 + 4*delta_X2 + delta
  24.                 let q4 = delta_X3 - delta_X2
  25.    
  26.                 let x = 0.5 * ((points[ p0 ].x * q1)  +  (points[ p1 ].x * q2)  +  (points[ p2 ].x * q3)  +  (points[ p3 ].x * q4))
  27.                 let y = 0.5 * ((points[ p0 ].y * q1)  +  (points[ p1 ].y * q2)  +  (points[ p2 ].y * q3)  +  (points[ p3 ].y * q4))
  28.    
  29.                 return {x,y}
  30.             }
  31.            
  32.             function setupCanvas(canvas) {
  33.                 // Get the device pixel ratio, falling back to 1.
  34.                 let dpr = window.devicePixelRatio || 1;
  35.                
  36.                 // Get the size of the canvas in CSS pixels.
  37.                 let rect = canvas.getBoundingClientRect();
  38.                
  39.                 // Give the canvas pixel dimensions of their CSS
  40.                 // size * the device pixel ratio.
  41.                 canvas.width = rect.width * dpr;
  42.                 canvas.height = rect.height * dpr;
  43.                 let ctx = canvas.getContext('2d');
  44.                
  45.                 // Scale all drawing operations by the dpr, so you
  46.                 // don't have to worry about the difference.
  47.                 ctx.scale(dpr, dpr);
  48.                 return ctx;
  49.             }
  50.            
  51.             function path() {
  52.                 for (let i=0; i < points.length; i+= 1/255) {
  53.                     let {x, y} = spline(points, i)
  54.                     draw(x,y, "#3d3d3d", 2 )
  55.                 }
  56.             }
  57.            
  58.             function clean() {
  59.                 ctx.clearRect(0, 0, canvas.width, canvas.height);
  60.                
  61.                 points.forEach( (point, index) => {
  62.                     draw(point.x,point.y)
  63.                 })
  64.             }
  65.            
  66.             function reset() {
  67.                 points = []
  68.                 clean()
  69.             }
  70.            
  71.             let canvas = document.getElementById("myCanvas");
  72.             let ctx = setupCanvas(canvas)
  73.            
  74.             let points = []
  75.            
  76.             canvas.addEventListener("click", eventData => {
  77.                 let rect = canvas.getBoundingClientRect()
  78.                 let x = event.clientX - rect.left
  79.                 let y = event.clientY - rect.top
  80.                
  81.                 points.push({x,y})
  82.                 clean()
  83.                 path()
  84.                 //callc()
  85.             })
  86.            
  87.             let total = 0
  88.            
  89.             function callc () {
  90.                 total = 0
  91.                 for (let i = 0; i < points.length; i++) {
  92.                     let currentP = points[i]
  93.                     let nextP = points[(i+1)%points.length]
  94.                    
  95.                     let oldp = currentP
  96.                     currentP.length = 0
  97.                     for (let t = 0; t < 1; t += 1/ 2500) {
  98.                         let newp = spline(points, i + t)
  99.                        
  100.                         currentP.length += Math.sqrt(
  101.         (newp.x - oldp.x) **2 +
  102.         (newp.y - oldp.y) **2
  103.     )
  104.    
  105.                        
  106.                         oldp = newp
  107.                     }
  108.                    
  109.                     currentP.length += Math.sqrt(
  110.         (nextP.x - oldp.x) **2 +
  111.         (nextP.y - oldp.y) **2
  112.     )
  113.                    
  114.                     total += currentP.length
  115.                 }
  116.                 document.getElementById("lenn").innerHTML = total
  117.             }
  118.            
  119.             let mark = 0
  120.            
  121.             function markk() {
  122.                 mark = Number(document.getElementById("slider").value)
  123.                 let {x, y} = spline(points, mark)
  124.                 clean()
  125.                 draw(x,y, "#017db1", 2 )
  126.             }
  127.            
  128.             function play() {
  129.                
  130.                 calls.push((dt) => {
  131.                    
  132.                    
  133.                     document.getElementById("lenn").innerHTML = dt
  134.                    
  135.                    
  136.                     let t = 0
  137.                     let m = mark
  138.                     while (m > points[t].length) {
  139.                         m -= points[t].length
  140.                         t++
  141.                     }
  142.                    
  143.                     let tt = t + (m / points[t].length)
  144.                    
  145.                     let {x, y} = spline(points, tt * dt)
  146.                     //clean()
  147.                     draw(x,y, "#017db1", 2 )
  148.                     document.getElementById("slider").value = mark
  149.                     mark = (mark + 0.5) % total
  150.                    
  151.                     if(mark >= total) {
  152. clean()
  153. path()
  154. calls.pop()}
  155.                 })
  156.             }
  157.            
  158.             let calls = []
  159.            
  160.             let last = new Date().getTime()
  161.             setInterval(function() {
  162.                 let now = new Date().getTime()
  163.                 dt = (now - last) / 50
  164.                 last = now
  165.                
  166.                 document.getElementById("point").textContent = points.length
  167.                 document.getElementById("slider").max = points.length
  168.                
  169.                 calls.forEach(i => i(dt))
  170.             }, 50)
Add Comment
Please, Sign In to add comment