637275

Untitled

Sep 9th, 2016
292
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <meta charset="utf-8">
  5.         <title>distance estimation</title>
  6.     </head>
  7.     <body>
  8.         <canvas id="canvas"></canvas>
  9. <script>
  10. //distance from a line segment
  11. //this also can be a <canvas> pixel shader boilerplate
  12.  
  13. //plot a pixel on arbitrary image data
  14. //image = ctx.createImageData...
  15. //color = rgba byte array
  16. function plot(image, x, y, color) {
  17.     var inx = (y * image.width * 4) + (x * 4);
  18.     image.data[inx]   = color[0];
  19.     image.data[++inx] = color[1];
  20.     image.data[++inx] = color[2];
  21.     image.data[++inx] = color[3];
  22.     return;
  23. }
  24.  
  25. function sqr(x) {
  26.     return x * x;
  27. }
  28.  
  29. //dist between two coords
  30. function dist(v, w) {
  31.     return Math.sqrt(sqr(v.x - w.x) + sqr(v.y - w.y));
  32. }
  33.  
  34. //clamp val to between min and max
  35. function clamp(val, min, max) {
  36.     return Math.min(Math.max(val, min), max);
  37. }
  38.  
  39. //lerp interp is 0..1
  40. function lerp(binterp, a, b) {
  41.     binterp = clamp(binterp, 0, 1);
  42.     ainterp = 1 - binterp;
  43.     return a * ainterp + b * binterp;
  44. }
  45.  
  46. //like an advanced gray(): instead of mapping a val from black to white,
  47. //colormap() maps it between any two colors of your choice
  48. function colormap(val, a, b) {
  49.     val = clamp(val, 0, 255) / 255;
  50.     return [lerp(val, a[0], b[0]),
  51.         lerp(val, a[1], b[1]),
  52.         lerp(val, a[2], b[2]),
  53.         lerp(val, a[3], b[3])];
  54. }
  55.  
  56. //make gray color from byte
  57. function gray(v) {
  58.     v = clamp(v, 0, 0xff);
  59.     return [v, v, v, 0xff];
  60. }
  61.  
  62. //PUT UR CODE HERE!!!!
  63. //all the arguments of points that contain an x and y coord
  64. //access w/ p.x & p.y etc...
  65. //this must return a color: an array in the format
  66. //[red, green, blue, alpha] where each number is between 0 and 0xff (255)
  67. //the gray() function above converts a value from 0 to 0xff to a gray color
  68. function estimateDistance(p, v, w) {
  69.     period = (dist(p, w) / 20) + 15;
  70.     return gray(0xff
  71.         * Math.sin(p.x / period)
  72.         * Math.cos(p.y / period)
  73.         / (dist(p, v) / 100));
  74. }
  75.  
  76. (function() {
  77.     //initialize cvs, ctx, etc
  78.     var cvs = document.getElementById('canvas'),
  79.     ctx = cvs.getContext('2d'),
  80.     frameWidth = 650,
  81.     frameHeight = 650,
  82.     time = 0;
  83.  
  84.     cvs.setAttribute('height', frameHeight);
  85.     cvs.setAttribute('width', frameWidth);
  86.  
  87.     var render = function(time) {
  88.     //time is waaaaay too big, get it a bit smaller
  89.     time /= 200;
  90.     //set up the end points, make them wiggle with sin/cos
  91.     var v = {
  92.         x: 80  + 15 * Math.cos(time),
  93.         y: 120 + 15 * Math.sin(time)
  94.     };
  95.     //offset so both coordinates aren't on the same cycle
  96.     time += 1;
  97.     var w = {
  98.         x: 375 + 35 * Math.cos(time),
  99.         y: 400 + 35 * Math.sin(time)
  100.     };
  101.  
  102.     var p = {
  103.         x: 0,
  104.         y: 0
  105.     };
  106.  
  107.     //create pixel array
  108.     var image = ctx.createImageData(frameWidth, frameHeight);
  109.     //iterate through the image
  110.     for(var x = 0; x < frameWidth; x++) {
  111.         for(var y = 0; y < frameHeight; y++) {
  112.             p.x = x;
  113.             p.y = y;
  114.             //plot 100/distance as the brightness
  115.             plot(image, x, y, estimateDistance(p, v, w));
  116.             }
  117.         }
  118.     //push array to canvas
  119.     ctx.putImageData(image, 0, 0);
  120.     //update time, push a frame, call render() again
  121.     time = window.requestAnimationFrame(render);
  122.     };
  123.     //first call
  124.     render.call();
  125. }());
  126. </script>
  127.     </body>
  128. </html>
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment