Guest User

Untitled

a guest
Jan 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. element = document.getElementById "canvas1"
  2. c = element.getContext "2d"
  3.  
  4. width = parseInt element.getAttribute "width"
  5. height = parseInt element.getAttribute "height"
  6.  
  7. imageData = c.createImageData width, height;
  8.  
  9. ###
  10.  
  11. ###
  12.  
  13. randomAndMultiplyBy = (mul) -> parseInt Math.random() * mul
  14. randomFilter = ({r, g, b, a}) ->
  15. r = randomAndMultiplyBy 256
  16. g = randomAndMultiplyBy 256
  17. b = randomAndMultiplyBy 256
  18. color = {r, g, b, a: 0xff}
  19.  
  20. class Pixel
  21. constructor: (@imageData, @width, @height) ->
  22. @itemsPerX = 4
  23. @imgData = @imageData.data
  24. return this
  25. getPixelAt: ({x, y}) ->
  26. i = @getIndexAt x, y
  27. r = @imgData[i+0]
  28. g = @imgData[i+1]
  29. b = @imgData[i+2]
  30. a = @imgData[i+3]
  31. return {r, g, b, a}
  32. getIndexAt: (x, y) -> (x + y * @width) * @itemsPerX
  33. setPixelAt: ({x, y}, data) ->
  34. i = @getIndexAt x, y
  35. @imgData[i+0] = data.r
  36. @imgData[i+1] = data.g
  37. @imgData[i+2] = data.b
  38. @imgData[i+3] = data.a
  39. return this
  40. drawCanvas: () ->
  41. totalPoint = @width * @height
  42. for x in [0...@width]
  43. for y in [0...@height]
  44. @setPixelAt {x, y}, randomFilter @getPixelAt {x, y}
  45. return this
  46. getCanvasData: () -> @imageData
  47.  
  48.  
  49. p = new Pixel(imageData, width, height)
  50. cdata = p.drawCanvas().getCanvasData()
  51. console.log p.getPixelAt 0, 0
  52. console.log width, height
  53. # console.log p.drawCanvas()
  54. c.putImageData cdata, 0, 0
Add Comment
Please, Sign In to add comment