Skip to content
This repository has been archived by the owner on Jun 18, 2023. It is now read-only.

Commit

Permalink
feat(geometry): 2 more helpers (right bound and uniform padding) and …
Browse files Browse the repository at this point in the history
…fixed the bounds helper
  • Loading branch information
prescientmoon committed Oct 19, 2020
1 parent 8ba7f2a commit 3cfd719
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
14 changes: 7 additions & 7 deletions packages/geometry/src/Foreign.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const geom = require("@thi.ng/geom")
const hiccupCanvas = require("@thi.ng/hiccup-canvas")

exports.mkRect = (attribs) => ({ x, y }) => (width) => (height) =>
exports.mkRect = (attribs) => ({ x, y, width, height }) =>
geom.rect([x, y], [width, height], attribs)

exports.mkCircle = (attribs) => ({ x, y }) => (radius) =>
Expand All @@ -17,14 +17,14 @@ exports.mkGroup = (attribs) => (shapes) => geom.group(attribs, shapes)

exports.renderGeometry = (shape) => (ctx) => () => hiccupCanvas.draw(ctx, shape)

exports.fitIntoBoundsImpl = (shape) => {
const rect = geom.fitIntoBounds2(shape, geom.rect())
exports.boundsImpl = (shape) => {
const dest = geom.bounds(shape)

return {
x: rect.pos[0],
y: rect.pos[1],
width: rect.size[0],
height: rect.size[1]
x: dest.pos[0],
y: dest.pos[1],
width: dest.size[0],
height: dest.size[1]
}
}

Expand Down
18 changes: 12 additions & 6 deletions packages/geometry/src/Foreign.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module Lunarflow.Geometry.Foreign
( Geometry
, fromShape
, renderGeometry
, fitIntoBounds
, bounds
, getRightBound
) where

import Prelude
Expand All @@ -16,16 +17,21 @@ foreign import data Geometry :: Type
-- | Cast a purescript shape to a js geometry.
fromShape :: Shape -> Geometry
fromShape = case _ of
Rect attribs position width height -> mkRect attribs position width height
Rect attribs bounds' -> mkRect attribs bounds'
Circle attribs position radius -> mkCircle attribs position radius
Polygon attribs points -> mkPolygon attribs points
Group attribs shapes -> mkGroup attribs (fromShape <$> shapes)

-- | Find the smallest rect some shapes fit in.
fitIntoBounds :: Shape -> Bounds
fitIntoBounds = fitIntoBoundsImpl <<< fromShape
bounds :: Shape -> Bounds
bounds = boundsImpl <<< fromShape

foreign import mkRect :: CommonAttribs' -> { | Position () } -> Int -> Int -> Geometry
-- TODO: more efficient way
-- | Get the rightmost point in a shape
getRightBound :: Shape -> Int
getRightBound = (\{ width, x } -> x + width) <<< bounds

foreign import mkRect :: CommonAttribs' -> Bounds -> Geometry

foreign import mkCircle :: CommonAttribs' -> { | Position () } -> Int -> Geometry

Expand All @@ -35,5 +41,5 @@ foreign import mkGroup :: CommonAttribs' -> Array Geometry -> Geometry

foreign import renderGeometry :: Geometry -> Context2D -> Effect Unit

foreign import fitIntoBoundsImpl :: Geometry -> Bounds
foreign import boundsImpl :: Geometry -> Bounds
-- foreign import geometryToRectImpl :: Partial => Geometry -> CommonAttribs' -> Position' -> Int -> Int -> Shape
17 changes: 7 additions & 10 deletions packages/geometry/src/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ type Position'
type ShapeConstructor a
= forall r. PartialRow (CommonAttribs ()) r => Record r -> a

-- | Rect-like shape data.
type Bounds
= { x :: Int, y :: Int, height :: Int, width :: Int }

newtype PolygonAttribs
= PolygonAttribs (Array (Record (Position + ())))

Expand All @@ -36,7 +40,7 @@ instance debugPolygonAttribs :: Debug PolygonAttribs where
debug = genericDebug

data Shape
= Rect CommonAttribs' { | Position () } Int Int
= Rect CommonAttribs' Bounds
| Polygon CommonAttribs' PolygonAttribs
| Circle CommonAttribs' { | Position () } Int
| Group CommonAttribs' (Array Shape)
Expand All @@ -58,7 +62,7 @@ derive instance genericShape :: Generic Shape _
instance debugSemigruoup :: Debug Shape where
debug a = genericDebug a

rect :: ShapeConstructor ({ | Position () } -> Int -> Int -> Shape)
rect :: ShapeConstructor (Bounds -> Shape)
rect attribs = Rect (withDefaults defaultAttribs attribs)

polygon :: ShapeConstructor (PolygonAttribs -> Shape)
Expand All @@ -71,11 +75,4 @@ group :: ShapeConstructor (Array Shape -> Shape)
group attribs = Group (withDefaults defaultAttribs attribs)

defaultAttribs :: CommonAttribs'
defaultAttribs = { fill: "blue", stroke: "black" }

-- | Rect-like shape data.
type Bounds
= { x :: Int, y :: Int, height :: Int, width :: Int }

fromBounds :: ShapeConstructor (Bounds -> Shape)
fromBounds attribs bounds = rect attribs { x: bounds.x, y: bounds.y } bounds.width bounds.height
defaultAttribs = { fill: "transparent", stroke: "black" }
8 changes: 8 additions & 0 deletions packages/geometry/src/Utils.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Lunarflow.Geometry.Utils where

import Prelude
import Lunarflow.Geometry.Types (Bounds)

-- | Add uniform padding to some bounds.
withPadding :: Int -> Bounds -> Bounds
withPadding amount { x, y, width, height } = { x, y, width: width + amount, height: height + amount }

0 comments on commit 3cfd719

Please sign in to comment.