Skip to content

Commit

Permalink
Feature/disable sticker rotation (#8)
Browse files Browse the repository at this point in the history
* feat(Sticker): add disable rotation option

* docs: add disableRotation prop

* 3.0.2

* chore(demo): revert canvas size to square
  • Loading branch information
andrewrubin authored Sep 8, 2023
1 parent 53fa91a commit 77d516b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ If no `initialScale` is provided `defaultScale` will be used.

`number` default `0.3` | **optional**

#### disableRotation

Disables rotation of a given sticker.

`Boolean` default `false` | **optional**

#### onDelete

A callback function to be called when the delete button is clicked.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wethegit/preact-stickerbook",
"version": "3.0.1",
"version": "3.0.2",
"description": "Easily create collage apps that are accessible by default.",
"files": [
"dist/*"
Expand Down
1 change: 1 addition & 0 deletions src/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export function App() {
onPosition={onPositionSticker}
onScale={onScaleSticker}
onRotate={onRotateSticker}
disableRotation={sticker.disableRotation}
{...sticker}
/>
))}
Expand Down
12 changes: 9 additions & 3 deletions src/lib/sticker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default function Sticker({
initialRotation = null,
initialPosition = null,
defaultScale = 0.3,
disableRotation = false,
// hooks
onDelete,
onReorder,
Expand Down Expand Up @@ -203,11 +204,15 @@ export default function Sticker({
setScale((cur) => cur + 0.01 * multiplier)

// rotate left
if (key === '<' || key === ',')
if (key === '<' || key === ',') {
if (disableRotation) return
setRotation((cur) => cur - 0.01 * multiplier)
}
// rotate right
else if (key === '>' || key === '.')
else if (key === '>' || key === '.') {
if (disableRotation) return
setRotation((cur) => cur + 0.01 * multiplier)
}

// Align top
if (key === 'w') setPosition((cur) => new Vec2(cur.x, cur.y - bounds.top))
Expand Down Expand Up @@ -264,7 +269,8 @@ export default function Sticker({
let initalradius = Math.min(imageDetails.x, imageDetails.y)

// Update the rotation / scale of the sticker
setRotationOffset(mousePosition.angle - ROTATION_BUTTON_OFFSET)
if (!disableRotation)
setRotationOffset(mousePosition.angle - ROTATION_BUTTON_OFFSET)
setScale(
(mousePosition.length + (radius - radius * controlsScale)) /
(initalradius * 0.5)
Expand Down

0 comments on commit 77d516b

Please sign in to comment.