Skip to content
Permalink
Browse files

Clean up point, size, frame utilities

  • Loading branch information
koenbok committed Apr 30, 2016
1 parent ff8eb4d commit ae6df890e0b790f5727e8bb8ffd5aba4e1e9abad
Showing with 87 additions and 24 deletions.
  1. +44 −17 framer/Layer.coffee
  2. +43 −7 framer/Utils.coffee
@@ -303,28 +303,55 @@ class exports.Layer extends BaseClass
##############################################################
# Geometry

# @define "point",
# get: -> _.pick(@, ["x", "y"])
# set: (point) ->
# return if not point
# point = {x: point, y: point} if _.isNumber(point)
# for k in ["x", "y"]
# @[k] = point[k] if point.hasOwnProperty(k)

# @define "size",
# get: -> _.pick(@, ["width", "height"])
# set: (size) ->
# return if not size
# size = {width: size, height: size} if _.isNumber(size)
# for k in ["width", "height"]
# @[k] = size[k] if size.hasOwnProperty(k)

# @define "frame",
# get: -> _.pick(@, ["x", "y", "width", "height"])
# set: (frame) ->
# return if not frame
# for k in ["x", "y", "width", "height"]
# @[k] = frame[k] if frame.hasOwnProperty(k)

_setGeometryValues: (input, keys) ->

# If this is a number, we set everything to that number
if _.isNumber(input)
for k in keys
@[k] = input
else

# If there is nothing to work with we exit
return unless input

# Set every numeric value for eacht key
for k in keys
@[k] = input[k] if _.isNumber(input[k])

@define "point",
get: -> _.pick(@, ["x", "y"])
set: (point) ->
return if not point
point = {x: point, y: point} if _.isNumber(point)
for k in ["x", "y"]
@[k] = point[k] if point.hasOwnProperty(k)
get: -> Utils.point(@)
set: (input) -> @_setGeometryValues(input, ["x", "y"])

@define "size",
get: -> _.pick(@, ["width", "height"])
set: (size) ->
return if not size
size = {width: size, height: size} if _.isNumber(size)
for k in ["width", "height"]
@[k] = size[k] if size.hasOwnProperty(k)
get: -> Utils.size(@)
set: (input) -> @_setGeometryValues(input, ["width", "height"])

@define "frame",
get: -> _.pick(@, ["x", "y", "width", "height"])
set: (frame) ->
return if not frame
for k in ["x", "y", "width", "height"]
@[k] = frame[k] if frame.hasOwnProperty(k)
get: -> Utils.frame(@)
set: (input) -> @_setGeometryValues(input, ["x", "y", "width", "height"])

@define "minX",
importable: true
@@ -577,6 +577,21 @@ Utils.loadImage = (url, callback, context) ->

# Point

Utils.point = (input) ->

return Utils.pointZero(input) if _.isNumber(input)
return Utils.pointZero() unless input

result = Utils.sizeZero()

for k in ["x", "y"]
result[k] = input[k] if _.isNumber(input[k])

return result

Utils.pointZero = (n=0) ->
return {x:n, y:n}

Utils.pointDivide = (point, fraction) ->
return point =
x: point.x / fraction
@@ -592,9 +607,6 @@ Utils.pointSubtract = (pointA, pointB) ->
x: pointA.x - pointB.x
y: pointA.y - pointB.y

Utils.pointZero = (args={}) ->
return _.defaults(args, {x:0, y:0})

Utils.pointMin = ->
points = Utils.arrayFromArguments arguments
point =
@@ -646,8 +658,20 @@ Utils.pointAngle = (pointA, pointB) ->

# Size

Utils.sizeZero = (args={}) ->
return _.defaults(args, {width:0, height:0})
Utils.size = (input) ->

return Utils.sizeZero(input) if _.isNumber(input)
return Utils.sizeZero() unless input

result = Utils.sizeZero()

for k in ["width", "height"]
result[k] = input[k] if _.isNumber(input[k])

return result

Utils.sizeZero = (n=0)->
return {width:n, height:n}

Utils.sizeMin = ->
sizes = Utils.arrayFromArguments arguments
@@ -709,8 +733,20 @@ Utils.frameGetMaxY = (frame) ->
Utils.frameSetMaxY = (frame, value) ->
frame.y = if frame.height is 0 then 0 else value - frame.height

Utils.frameZero = (args={}) ->
return _.defaults(args, {top:0, right:0, bottom:0, left:0})
Utils.frame = (input) ->

return Utils.frameZero(input) if _.isNumber(input)
return Utils.frameZero() unless input

result = Utils.frameZero()

for k in ["x", "y", "width", "height"]
result[k] = input[k] if _.isNumber(input[k])

return result

Utils.frameZero = (n=0) ->
return {x:n, y:n}

Utils.frameSize = (frame) ->
size =

0 comments on commit ae6df89

Please sign in to comment.
You can’t perform that action at this time.