-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
311 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Luna Mask Editor | ||
|
||
Image mask editing. | ||
|
||
## Demo | ||
|
||
https://luna.liriliri.io/?path=/story/mask-editor | ||
|
||
## Install | ||
|
||
Add the following script and style to your page. | ||
|
||
```html | ||
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/luna-toolbar/luna-toolbar.css" /> | ||
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/luna-painter/luna-painter.css" /> | ||
<script src="//cdn.jsdelivr.net/npm/luna-toolbar/luna-toolbar.js"></script> | ||
<script src="//cdn.jsdelivr.net/npm/luna-painter/luna-painter.js"></script> | ||
<script src="//cdn.jsdelivr.net/npm/luna-mask-editor/luna-mask-editor.js"></script> | ||
``` | ||
|
||
You can also get it on npm. | ||
|
||
```bash | ||
npm install luna-mask-editor luna-painter luna-toolbar --save | ||
``` | ||
|
||
```javascript | ||
import 'luna-toolbar/luna-toolbar.css' | ||
import 'luna-painter/luna-painter.css' | ||
import LunaMaskEditor from 'luna-mask-editor' | ||
``` | ||
|
||
## Usage | ||
|
||
```javascript | ||
const container = document.getElementById('container') | ||
const maskEditor = new LunaMaskEditor(container) | ||
``` | ||
|
||
## Configuration | ||
|
||
* image(string): Image src. | ||
|
||
## Api | ||
|
||
### getCanvas(): HTMLCanvasElement | ||
|
||
Get a canvas with mask drawn. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
"toolbar", | ||
"painter" | ||
], | ||
"style": false | ||
"style": false, | ||
"react": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { CSSProperties, FC, useEffect, useRef } from 'react' | ||
import MaskEditor, { IOptions } from './index' | ||
import { useNonInitialEffect } from '../share/hooks' | ||
|
||
interface IMaskEditorProps extends IOptions { | ||
style?: CSSProperties | ||
onCreate?: (maskEditor: MaskEditor) => void | ||
} | ||
|
||
const LunaMaskEditor: FC<IMaskEditorProps> = (props) => { | ||
const maskEditorRef = useRef<HTMLDivElement>(null) | ||
const maskEditor = useRef<MaskEditor>() | ||
|
||
useEffect(() => { | ||
const { image } = props | ||
maskEditor.current = new MaskEditor(maskEditorRef.current!, { | ||
image, | ||
}) | ||
props.onCreate && props.onCreate(maskEditor.current) | ||
|
||
return () => maskEditor.current?.destroy() | ||
}, []) | ||
|
||
useNonInitialEffect(() => { | ||
if (maskEditor.current) { | ||
maskEditor.current.setOption('image', props.image) | ||
} | ||
}, [props.image]) | ||
|
||
return <div ref={maskEditorRef} style={props.style}></div> | ||
} | ||
|
||
export default LunaMaskEditor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,98 @@ | ||
import MaskEditor from 'luna-mask-editor.js' | ||
import story from '../share/story' | ||
import $ from 'licia/$' | ||
import h from 'licia/h' | ||
import readme from './README.md' | ||
import LunaMaskEditor from './react' | ||
import { text } from '@storybook/addon-knobs' | ||
import { useRef } from 'react' | ||
|
||
const def = story('mask-editor', (container) => { | ||
$(container).css({ | ||
width: '100%', | ||
maxWidth: 1200, | ||
height: 600, | ||
margin: '0 auto', | ||
}) | ||
const def = story( | ||
'mask-editor', | ||
(wrapper) => { | ||
$(wrapper).html('') | ||
|
||
const maskEditor = new MaskEditor(container, { | ||
image: 'https://res.liriliri.io/luna/pic1.jpg', | ||
}) | ||
const container = h('div') | ||
|
||
$(container).css({ | ||
width: '100%', | ||
maxWidth: 1200, | ||
height: 600, | ||
margin: '0 auto', | ||
}) | ||
wrapper.appendChild(container) | ||
|
||
const maskContainer = h('div') | ||
$(maskContainer).css({ | ||
border: '1px solid #eee', | ||
width: '100%', | ||
maxWidth: 1200, | ||
margin: '0 auto', | ||
fontSize: 0, | ||
marginTop: '10px', | ||
}) | ||
wrapper.appendChild(maskContainer) | ||
|
||
const { image } = createKnobs() | ||
const maskEditor = new MaskEditor(container, { | ||
image, | ||
}) | ||
|
||
onCreate(maskEditor, maskContainer) | ||
|
||
return maskEditor | ||
}) | ||
return maskEditor | ||
}, | ||
{ | ||
readme, | ||
source: __STORY__, | ||
ReactComponent({ theme }) { | ||
const maskContainer = useRef(null) | ||
const { image } = createKnobs() | ||
|
||
return ( | ||
<> | ||
<LunaMaskEditor | ||
theme={theme} | ||
image={image} | ||
onCreate={(maskEditor) => { | ||
if (maskContainer.current) { | ||
onCreate(maskEditor, maskContainer.current) | ||
} | ||
}} | ||
/> | ||
<div | ||
style={{ | ||
border: '1px solid #eee', | ||
width: '100%', | ||
maxWidth: 1200, | ||
margin: '0 auto', | ||
fontSize: 0, | ||
marginTop: '10px', | ||
}} | ||
ref={maskContainer} | ||
></div> | ||
</> | ||
) | ||
}, | ||
} | ||
) | ||
|
||
function createKnobs() { | ||
const image = text('Image', 'https://res.liriliri.io/luna/pic1.jpg') | ||
|
||
return { | ||
image, | ||
} | ||
} | ||
|
||
function onCreate(maskEditor, maskContainer) { | ||
const canvas = maskEditor.getCanvas() | ||
maskContainer.appendChild(canvas) | ||
$(canvas).css({ | ||
maxWidth: '100%', | ||
}) | ||
} | ||
|
||
export default def | ||
|
||
export const { maskEditor } = def | ||
export const { maskEditor: html, react } = def |
Oops, something went wrong.