Skip to content

Instantly share code, notes, and snippets.

@obeattie
Created April 11, 2013 13:49
Show Gist options
  • Save obeattie/5363518 to your computer and use it in GitHub Desktop.
Save obeattie/5363518 to your computer and use it in GitHub Desktop.
CoffeeScript source for Paper.js modifications
_getClipItems: () ->
unless @_clipItems?
@_clipItems = (c for c in @_children when c._clipMask)
return @_clipItems
draw: (ctx, param) ->
clipItems = @_getClipItems()
# If the group is to be clipped, draw them to an in-memory canvas
if clipItems.length != 0
bounds = @getStrokeBounds()
return unless bounds.width and bounds.height # Group is invisible; don't continue
# Store previous offset and save the parent context so we can draw onto it later
prevOffset = param.offset
parentCtx = ctx
# Floor the offset and ceil the size, so we don't cut off any items that are subpixel-rendered
itemOffset = param.offset = bounds.topLeft.floor()
# Create a temporary canvas the size of the group to draw to
tempCanvas = CanvasProvider.getCanvas(bounds.size.ceil().add(Size.create(1, 1)))
ctx = tempCanvas.getContext('2d')
ctx.save()
# Translate the temporary context so the topLeft origin of items is at (0, 0)
ctx.translate(-itemOffset.x, -itemOffset.y)
# Apply our matrix to the temporary context
@_matrix.applyToContext(ctx)
for item in clipItems
Item.draw(item, ctx, param)
# Apply the clipping operation
ctx.globalCompositeOperation = @compositing
# Draw the regular items
for item in @_children
Item.draw(item, ctx, param) unless item._clipMask
# Draw the clipped items back to the original canvas
if tempCanvas?
parentCtx.drawImage(ctx.canvas, itemOffset.x, itemOffset.y)
ctx.restore()
CanvasProvider.release(tempCanvas)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment