Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/rotateBoundingBox.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Caman.Plugin.register 'rotateBoundingBox', (degrees) ->
angle = (degrees % 360 + 360 ) % 360
if angle == 0
return @dimensions =
width: @canvas.width
height: @canvas.height

toRadians = Math.PI / 180

if exports?
canvas = new Canvas()
else
canvas = document.createElement 'canvas'
Util.copyAttributes @canvas, canvas

switch
when angle <= 90
theta = angle * toRadians
width = @canvas.width * Math.cos(theta) + @canvas.height * Math.sin(theta)
height = @canvas.height * Math.cos(theta) + @canvas.width * Math.sin(theta)
x = @canvas.height * Math.sin(theta)
y = 0
when 90 < angle <= 180
theta = (angle - 90) * toRadians
width = @canvas.height * Math.cos(theta) + @canvas.width * Math.sin(theta)
height = @canvas.width * Math.cos(theta) + @canvas.height * Math.sin(theta)
x = @canvas.width * Math.sin(theta) + @canvas.height * Math.cos(theta)
y = @canvas.height * Math.sin(theta)
when 180 < angle <= 270
theta = (angle - 180) * toRadians
width = @canvas.width * Math.cos(theta) + @canvas.height * Math.sin(theta)
height = @canvas.height * Math.cos(theta) + @canvas.width * Math.sin(theta)
x = @canvas.width * Math.cos(theta)
y = @canvas.width * Math.sin(theta) + @canvas.height * Math.cos(theta)
else
theta = (angle - 270) * toRadians
width = @canvas.height * Math.cos(theta) + @canvas.width * Math.sin(theta)
height = @canvas.width * Math.cos(theta) + @canvas.height * Math.sin(theta)
x = 0
y = @canvas.width * Math.cos(theta)

canvas.width = width
canvas.height = height
ctx = canvas.getContext '2d'
ctx.save()
ctx.translate x, y
ctx.rotate angle * toRadians
ctx.drawImage @canvas, 0, 0, @canvas.width, @canvas.height
ctx.restore()

@replaceCanvas canvas

caman.Filter.register 'rotateBoundingBox', ->
@processPlugin 'rotateBoundingBox', Array.prototype.slice.call(arguments, 0)
25 changes: 25 additions & 0 deletions src/rotateSquareBox.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Caman.Plugin.register "rotateSquareBox", (degrees) ->
angle = degrees%360
toRadians = Math.PI/180

if exports?
canvas = new Canvas()
else
canvas = document.createElement 'canvas'
Util.copyAttributes @canvas, canvas

width = Math.sqrt(Math.pow(@originalWidth, 2) + Math.pow(@originalHeight, 2))
height = width
canvas.width = width
canvas.height = height
ctx = canvas.getContext '2d'
ctx.save()
ctx.translate width/2, height/2
ctx.rotate angle*toRadians
ctx.drawImage @canvas, -@canvas.width/2, -@canvas.height/2, @canvas.width, @canvas.height
ctx.restore()

@replaceCanvas canvas

Caman.Filter.register "rotateSquareBox", ->
@processPlugin "rotateSquareBox", Array.prototype.slice.call(arguments, 0)