Skip to content

Commit

Permalink
update version & complie fix & change interface
Browse files Browse the repository at this point in the history
  • Loading branch information
luz-alphacode committed Apr 18, 2020
1 parent 579518f commit e650afe
Show file tree
Hide file tree
Showing 10 changed files with 727 additions and 837 deletions.
34 changes: 24 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Paperjs Offset
The dicussion to implement a offset function in paper.js started years ago, yet the author have not decided to put a offset feature into the library. So I implement an extension of my own.
<br/>As far as I know, the author has promised recently to implement a native offset functionality in near feature, the library will be closed once the native implement is published.
<br/>This library implement both path offset and stroke offset, you may offset a path or expand a stroke like what you did in Adobe illustrator. Offset complicate path may cause unwanted self intersections, this library already take care some cases but bugs still exists. Please let me notice the false conditions in the issue pannel so I can correct it.

## Usage
Expand All @@ -10,18 +11,37 @@ npm install paperjs-offset
And then, in you project:
```javascript
import paper from 'paper'
import ExtendPaperJs from 'paperjs-offset'
import { PaperOffset } from 'paperjs-offset'

// call offset
PaperOffset.offset(path, offset, options)

// call offset stroke
PaperOffset.offsetStroke(path, offset, options)
```

You may still use the old way to extend paperjs module, which is **deprecated** and will be removed in future version.
```javascript
import ExtendPaperJs from 'paperjs-offset'
// extend paper.Path, paper.CompoundPath with offset, offsetStroke method
ExtendPaperJs(paper)
```

Or for web development, include the **paperjs-offset.js** or **paperjs-offset.min.js** in demo folder.
<br/>The library extends **paper.Path** and **paper.CompoundPath** object, with offset/offsetStroke functions.
<br/>The library now exposes a global variable **PaperOffset**, again, the extension of **paper.Path** and **paper.CompoundPath** with offset/offsetStroke functions is still available, but no longer recommended.
```javascript
let path = new paper.Path(/* params */)

PaperOffset.offset(path, 10, { join: 'round' })
PaperOffset.offsetStroke(path, 10, { cap: 'round' })

// deprecated
path.offset(10, { join: 'round' })
// deprecated
path.offsetStroke(10, { cap: 'round' })
```
Both offset/offsetStroke take the form of **f(offset: number, options?: {})**, the options have following parameters:

Both offset/offsetStroke take the form of **f(path: Path | CompoundPath, offset: number, options?: {})**, the options have following parameters:
<br/>&nbsp;&nbsp;**join**: the join style of offset path, you can choose **'miter'**, **'bevel'** or **'round'**, default is **'miter'**.
<br/>&nbsp;&nbsp;**limit**: the limit for miter style (refer the miterLimit's definition in paper.js).
<br/>&nbsp;&nbsp;**cap**: the cap style of offset (only available in offsetStroke), you can choose **'butt'** and **'round'** (**'square'** is not supported yet), default is **'butt'**
Expand All @@ -31,13 +51,7 @@ Both offset/offsetStroke take the form of **f(offset: number, options?: {})**, t
There are some cases that the library may return weird result or failed silently, please let me noticed in the project issues. And in some cases the library will yeild an ok result than a perfect one. Currently the library should give good results for closed shapes, but may fail in some open curve cases, I'm still working on it.
![Preview](/public/preview.jpg)

## Development
```sh
# build es5/umd/iife packages
npm run build
```
You can use open demo folder for simple cases demonstration.
<br/>*I've noticed there is a react adaption of paper.js but no vue adaption, so I create a vue adaption of paper.js, you can visit this [repo](https://github.com/luz-alphacode/paper-vueify) if you are interested.*

## License
Distributed under the MIT license. See [LICENSE](https://github.com/luz-alphacode/paperjs-offset/blob/master/LICENSE) for detail.
Distributed under the MIT license. See [LICENSE](https://github.com/glenzli/paperjs-offset/blob/master/LICENSE) for detail.
49 changes: 25 additions & 24 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,48 @@

// simple polygon
let r = new paper.Path.Rectangle({ point: [-500, -300], size: [80, 80], fillColor: 'rgb(191, 91, 91, 0.5)', strokeColor: 'black' })
r.offset(10)
PaperOffset.offset(r, 10)
r.bringToFront()
r.offset(-10).offset(-10).offset(-10)
PaperOffset.offset(PaperOffset.offset(PaperOffset.offset(r, -10), -10), -10);

// simple polygon + bevel
let r11 = new paper.Path.Rectangle({ point: [-500, -150], size: [60, 60], fillColor: 'rgb(191, 91, 91, 0.5)', strokeColor: 'black' })
let r12 = r11.offset(-10, { insert: false })
let r12 = PaperOffset.offset(r11, -10, { insert: false })
let r1 = r11.subtract(r12, { insert: true })
r11.remove()
r1.offset(15, { join: 'bevel' })
PaperOffset.offset(r1, 15, { join: 'bevel' })
r1.bringToFront()

// simple polygon + round
let r21 = new paper.Path.Rectangle({ point: [-350, -150], size: [60, 60], fillColor: 'rgb(191, 91, 91, 0.5)', strokeColor: 'black' })
let r22 = r21.offset(-10, { insert: false })
let r22 = PaperOffset.offset(r21, -10, { insert: false })
let r2 = r21.subtract(r22, { insert: true })
r21.remove()
r2.offset(15, { join: 'round' })
PaperOffset.offset(r2, 15, { join: 'round' })
r2.bringToFront()

// simple polygon
let s = new paper.Path.Star({ center: [-300, -260], points: 12, radius1: 40, radius2: 30, fillColor: 'rgba(234, 154, 100, 0.5)', strokeColor: 'black' })
s.offset(10)
PaperOffset.offset(s, 10)
s.bringToFront()
s.offset(-10).offset(-10)
PaperOffset.offset(PaperOffset.offset(s, -10), -10);

// smooth
let s2 = new paper.Path.Star({ center: [-150, -260], points: 7, radius1: 40, radius2: 30, fillColor: 'rgba(239, 209, 88, 0.5)', strokeColor: 'black' })
s2.smooth()
s2.offset(10)
PaperOffset.offset(s2, 10);
s2.bringToFront()
s2.offset(-10).offset(-10)
PaperOffset.offset(PaperOffset.offset(s2, -10), -10)

// complex
let c1 = new paper.Path.Circle({ center: [-20, -260], radius: 40, fillColor: 'rgba(165, 193, 93, 0.5)', strokeColor: 'black' })
let c2 = new paper.Path.Circle({ center: [50, -260], radius: 40, fillColor: 'rgba(165, 193, 93, 0.5)', strokeColor: 'black' })
let c = c1.unite(c2, { insert: true })
c1.remove()
c2.remove()
c.offset(10)
PaperOffset.offset(c, 10);
c.bringToFront()
c.offset(-10).offset(-10).offset(-10)
PaperOffset.offset(PaperOffset.offset(PaperOffset.offset(c, -10), -10), -10)

let c3 = new paper.Path.Circle({ center: [180, -260], radius: 40, fillColor: 'rgba(117, 170, 173, 0.5)', strokeColor: 'black' })
let c4 = new paper.Path.Circle({ center: [230, -260], radius: 40, fillColor: 'rgba(117, 170, 173, 0.5)', strokeColor: 'black' })
Expand All @@ -58,9 +58,9 @@
c4.remove()
c5.remove()
cc1.remove()
cc.offset(10)
PaperOffset.offset(cc, 10)
cc.bringToFront()
cc.offset(-10).offset(-10).offset(-10).offset(-5)
PaperOffset.offset(PaperOffset.offset(PaperOffset.offset(PaperOffset.offset(cc, -10), -10), -10), -5)

// complex+
let c6 = new paper.Path.Circle({ center: [380, -260], radius: 40, fillColor: 'rgba(156, 104, 193, 0.5)', strokeColor: 'black' })
Expand All @@ -75,17 +75,17 @@
ccc.smooth()
ccc.offset(10)
ccc.bringToFront()
ccc.offset(-10).offset(-10)
ccc.offset(-30).offset(-5)
PaperOffset.offset(PaperOffset.offset(ccc, -10), -10)
PaperOffset.offset(PaperOffset.offset(ccc, -30), -5)

// stroke
let rs = new paper.Path.Rectangle({ point: [-200, -150], size: [80, 80], fillColor: null, strokeColor: 'rgb(191, 91, 91, 0.5)' })
rs.offsetStroke(10)
PaperOffset.offsetStroke(rs, 10)
rs.bringToFront()

// stroke
let st1 = new paper.Path.Line({ from: [-50, -100], to: [0, -100], strokeColor: 'rgba(156, 104, 193, 0.5)', strokeWidth: 3 })
st1.offsetStroke(20, { cap: 'round' })
PaperOffset.offsetStroke(st1, 20, { cap: 'round' })
st1.bringToFront()

// stroke complex
Expand All @@ -95,30 +95,31 @@
cs.fillColor = null
cs.position = [150, -50]
cs.closed = false
cs.offsetStroke(20)
PaperOffset.offsetStroke(cs, 20)
cs.bringToFront()
let cs2 = cs.clone()
cs2.position = [400, -50]
cs2.strokeColor = 'rgba(117, 170, 173, 0.5)'
cs2.offsetStroke(25, { cap: 'round' })
PaperOffset.offsetStroke(cs2, 25, { cap: 'round' })
cs2.bringToFront()

// edge cases
let ec1 = new paper.Path({ pathData: 'M466,467c0,0 -105,-235 0,0c-376.816,-119.63846 -469.06596,-146.09389 -650.61329,-266.59735c-282.68388,-230.49081 300.86045,-10.26825 452.77726,121.52815z', fillColor: 'rgba(156, 104, 193, 0.5)' })
ec1.translate(-450, -250)
ec1.scale(0.4)
ec1.offset(10)
ec1.offset(-10).offset(-10).offset(-10)
PaperOffset.offset(ec1, 10)
PaperOffset.offset(PaperOffset.offset(PaperOffset.offset(ec1, -10), -10), -10)

let ec2 = new paper.Path({ pathData: 'M466,467c-65,-34 136,64 0,0c-391,-270 62,-670 62,-670l-463,370z', strokeColor: 'rgba(239, 209, 88, 0.5)', strokeWidth: 3 })
ec2.scale(0.4)
ec2.translate(-350, 20)
ec2.offsetStroke(10)
PaperOffset.offsetStroke(ec2, 10)

let ec3 = new paper.Path({ pathData: 'M466,467c-65,-34 136,64 0,0c-391,-270 520,-471 522,-137c-214,-144 -1489,123 -923,-163z', fillColor: 'rgb(191, 91, 91, 0.5)' })
ec3.scale(0.4)
ec3.translate(-100, -150)
ec3.offset(-10)
PaperOffset.offset(ec3, -10)
}

window.onload = RunDemo
})()
Loading

0 comments on commit e650afe

Please sign in to comment.