Guest User

Untitled

a guest
Jun 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. window.addEventListener "DOMContentLoaded", ->
  2. body = $ "body"
  3. canvas = $ "#canvas"
  4. chalkboard = $ "#chalkboard"
  5. close = $ "#close"
  6. ledge = $ "#ledge"
  7. lightswitch = $ "#lightswitch"
  8. output = $ "#output"
  9. shade = $ "#shade"
  10. share = $ "#share"
  11.  
  12. canvas.attr "width", chalkboard.width()
  13. canvas.attr "height", chalkboard.height()
  14. xOffset = yOffset = 0
  15.  
  16. context = canvas.get(0).getContext "2d"
  17. context.lineWidth = 8
  18. context.lineCap = "round"
  19. context.lineJoin = "miter"
  20.  
  21. background = new Image
  22. background.src = "/images/background.jpg"
  23. background.onload = ->
  24. context.drawImage background, -128, -129
  25.  
  26. updateOffsets = ->
  27. offsets = chalkboard.offset()
  28. xOffset = offsets.left
  29. yOffset = offsets.top
  30.  
  31. window.addEventListener "orientationchange", updateOffsets
  32. updateOffsets()
  33.  
  34. window.addEventListener "touchmove", (event) ->
  35. event.preventDefault()
  36.  
  37.  
  38.  
  39. ### Tools ###
  40.  
  41. createPattern = (name, callback) ->
  42. image = new Image
  43. image.src = "/images/chalk-tile-#{name}"
  44. image.onload = -> callback context.createPattern image, "repeat"
  45.  
  46. setStroke = (pattern, width) ->
  47. context.strokeStyle = pattern
  48. context.lineWidth = width
  49.  
  50. whitePattern = redPattern = erasePattern = null
  51. createPattern "white.png", (p) -> setStroke whitePattern = p, 8
  52. createPattern "red.png", (p) -> redPattern = p
  53. createPattern "erase.jpg", (p) -> erasePattern = p
  54.  
  55. tools = ["eraser", "red_chalk", "white_chalk"]
  56. activeTool = ""
  57.  
  58. activateTool = (tool) ->
  59. return if tool is activeTool
  60.  
  61. tools.splice tools.indexOf(tool), 1
  62. tools.push activeTool = tool
  63. $("##{id}, ##{id}_tool").css("z-index", index) for id, index in tools
  64. $("#tools div.tool").removeClass "active"
  65. $("##{id}_tool").addClass "active"
  66.  
  67. switch tool
  68. when "red_chalk" then context.strokeStyle = setStroke redPattern, 8
  69. when "white_chalk" then context.strokeStyle = setStroke whitePattern, 8
  70. when "eraser" then context.strokeStyle = setStroke erasePattern, 32
  71.  
  72. activateTool "white_chalk"
  73.  
  74. ledge.delegate "a", "click", (target) ->
  75. activateTool $(target).attr "id"
  76.  
  77.  
  78.  
  79. ### Drawing ###
  80.  
  81. skip = false
  82. count = 0
  83. points = [null]
  84. x = y = null
  85.  
  86. draw = (point) ->
  87. if point
  88. if skip
  89. skip = false
  90. else
  91. context.moveTo x, y
  92. [x, y] = point
  93. context.lineTo x, y
  94. else
  95. skip = true
  96.  
  97. canvas.bind "touchstart", (event) ->
  98. touch = event.touches[0]
  99. [x, y] = [touch.pageX - xOffset, touch.pageY - yOffset]
  100. event.preventDefault()
  101.  
  102. canvas.bind "touchmove", (event) ->
  103. touch = event.touches[0]
  104. points.push [touch.pageX - xOffset, touch.pageY - yOffset]
  105.  
  106. canvas.bind "touchend", (event) ->
  107. points.push [x, y], [x, y], null
  108.  
  109. setInterval ->
  110. return unless points.length
  111. start = new Date
  112. context.beginPath()
  113. draw points.shift() while points.length and new Date - start < 10
  114. context.stroke()
  115. , 30
  116.  
  117.  
  118.  
  119. ### Shade ###
  120.  
  121. lightswitch.bind "click", (event) ->
  122. if body.hasClass "shade"
  123. body.removeClass "shade"
  124. else
  125. body.addClass "shade"
  126. event.preventDefault()
  127.  
  128.  
  129.  
  130. ### Share ###
  131.  
  132. openShareWindow = ->
  133. share.addClass "active"
  134. setTimeout ->
  135. output.attr "src", canvas.get(0).toDataURL()
  136. output.get(0).onload = ->
  137. body.addClass "share"
  138. , 10
  139.  
  140. closeShareWindow = ->
  141. share.removeClass "active"
  142. body.removeClass "share"
  143. output.get(0).onload = null
  144. output.attr "src", "/images/chalk-sprites.png"
  145.  
  146. share.bind "touchstart", ->
  147. openShareWindow()
  148.  
  149. close.bind "click", (event) ->
  150. closeShareWindow()
  151. event.preventDefault()
  152.  
  153. output.bind "touchcancel", ->
  154. setTimeout closeShareWindow, 50
Add Comment
Please, Sign In to add comment