|
| 1 | +# [Mojs Curve Editor](https://github.com/mojs/mojs-curve-editor) |
| 2 | +[](https://www.npmjs.com/package/@mojs/curve-editor) |
| 3 | + |
| 4 | +> `MojsCurveEditor` is a GUI plugin for interactive **custom easings / property curves** editing while crafting your animations. Part of mojs tools. |
| 5 | +
|
| 6 | +## TLDR; |
| 7 | +* Install it: `npm i @mojs/curve-editor` |
| 8 | +* Import it: `import MojsCurveEditor from '@mojs/curve-editor';`' |
| 9 | +* Use it: |
| 10 | +```js |
| 11 | +const mojsCurve = new MojsCurveEditor(); |
| 12 | + |
| 13 | +const tween = new mojs.Tween({ |
| 14 | + easing: mojsCurve.getEasing() |
| 15 | +}); |
| 16 | +``` |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +<MojsCurveEditorExample dark /> |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +The `MojsCurveEditor` depends on `mojs >= 0.225.2`, tween autoupdates available for `mojs >= 0.276.2`. Please make sure you've imported [mojs](https://github.com/mojs/mojs) library first. |
| 25 | + |
| 26 | +### cdn |
| 27 | +```html |
| 28 | +<script src="https://cdn.jsdelivr.net/npm/@mojs/curve-editor"></script> |
| 29 | +``` |
| 30 | +### npm |
| 31 | +```bash |
| 32 | +npm i @mojs/curve-editor |
| 33 | +``` |
| 34 | + |
| 35 | +Import `MojsCurveEditor` into your code: |
| 36 | + |
| 37 | +```js |
| 38 | +import MojsCurveEditor from '@mojs/curve-editor'; |
| 39 | +``` |
| 40 | + |
| 41 | +::: tip |
| 42 | +If you installed it with the CDN option - you should have `MojsCurveEditor` globally. |
| 43 | +::: |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +Construct `MojsCurveEditor` and provide a name of the curve you're working on: |
| 48 | + |
| 49 | +```js{2} |
| 50 | + const mojsCurve = new MojsCurveEditor({ |
| 51 | + name: 'bounce curve' |
| 52 | + }); |
| 53 | +``` |
| 54 | +::: tip |
| 55 | +The name is used to |
| 56 | +identify record in `localStorage` to restore the state from |
| 57 | +when page gets reloaded, so please specify unique names if |
| 58 | +you use more than one editor on the same page. |
| 59 | +::: |
| 60 | + |
| 61 | +After that you can "connect" the curve with your `mojs` modules by passing a "sample" of the curve to the `easing` property of the module, like this: |
| 62 | + |
| 63 | +```js |
| 64 | + const myCurve = new MojsCurveEditor({name: 'myCurve'}); |
| 65 | + |
| 66 | + const tween = new mojs.Tween({ |
| 67 | + easing: myCurve.getEasing() |
| 68 | + }); |
| 69 | + |
| 70 | + // or |
| 71 | + |
| 72 | + const shape = new mojs.Shape({ |
| 73 | + easing: myCurve.getEasing() |
| 74 | + }); |
| 75 | + |
| 76 | + // or as `property curve` |
| 77 | + |
| 78 | + const html = new mojs.Html({ |
| 79 | + el: '#js-el', |
| 80 | + x: { 0: 100, curve: myCurve.getEasing() } |
| 81 | + }); |
| 82 | + |
| 83 | +``` |
| 84 | + |
| 85 | +Each `tween`/`module` should have it's out sample of the curve, this means you need to call `myCurve.getEasing()` send the `sample` of the curve to the `easing` property of modules. |
| 86 | + |
| 87 | +If you use `mojs>0.276.5` the state of the modules with the curve `sample` will be updated automatically. |
| 88 | + |
| 89 | +The `getEasing` function receives options hash: |
| 90 | + |
| 91 | +```js |
| 92 | + easing: myCurve.getEasing({ |
| 93 | + // `transform` function that pipes through the current value |
| 94 | + // of the curve so you can transform it |
| 95 | + transform: (k) => { return k; } |
| 96 | + }); |
| 97 | + |
| 98 | +``` |
| 99 | + |
| 100 | +After you are happy with the curve you made, you need to change the **sample** (the `myCurve.getEasing()` call) with the actual path data which you can get by clicking on the `code` button ( <svg width="32" style="position: relative; top: 0.4em; width: 24px; height: 24px; display: inline-block; background: #fff; border-radius: 3px; box-shadow: 1px 1px 0 rgba(0,0,0,.15);" viewBox="0 0 32 32"><use xlink:href="#maximize-shape"></use></svg> ): |
| 101 | + |
| 102 | +```js |
| 103 | + const html = new mojs.Html({ |
| 104 | + el: '#js-el', |
| 105 | + x: { 0: 100, easing: 'M0, 100 C0, 100 19.8984745544779, 40.10152544552211 30, 30 C40.1015254455221, 19.89847455447789 80, 45 80, 45 C80, 45 100, 0 100, 0 ' } |
| 106 | + }); |
| 107 | +``` |
| 108 | + |
| 109 | +## Options |
| 110 | + |
| 111 | +```js |
| 112 | +const curveEditor = new MojsCurveEditor({ |
| 113 | + // name of the curve editor |
| 114 | + name: 'bounce curve', |
| 115 | + |
| 116 | + // if should preserve state on page reloads |
| 117 | + isSaveState: true, |
| 118 | + |
| 119 | + // start path - will be loaded on initialization of the curve, |
| 120 | + // e.g. before any user modifications were made. Path of 'M0, 100 L100, 0' is set by default. |
| 121 | + startPath: 'M0, 100 L100, 0', |
| 122 | + |
| 123 | + // callback on path change, accepts path string |
| 124 | + onChange: function (path) {}, |
| 125 | + |
| 126 | + // if should hide when minimized - useful when you try to embed |
| 127 | + isHiddenOnMin: false |
| 128 | +}); |
| 129 | +``` |
| 130 | + |
| 131 | +## Public Methods |
| 132 | + |
| 133 | +```js |
| 134 | +curveEditor |
| 135 | + |
| 136 | + // get `easing function` of the curve |
| 137 | + .getEasing() |
| 138 | + |
| 139 | + // maximize the curve editor |
| 140 | + .maximize() |
| 141 | + |
| 142 | + // minimize the curve editor |
| 143 | + .minimize() |
| 144 | + |
| 145 | + // toggle `maximize/minimize` methods regarding editor's state |
| 146 | + .toggleSize(); |
| 147 | +``` |
| 148 | + |
| 149 | +## Shortcuts |
| 150 | + |
| 151 | +- `alt + z` - **undo** curve action |
| 152 | +- `alt + x` - **redo** curve action |
| 153 | +- `alt + d` - **delete** selected point(s) |
| 154 | +- [3 times] `alt + \` - **reset** curve |
| 155 | + |
| 156 | +::: tip |
| 157 | +The shortcuts works only in the active editor - it should have **orange mojs logo indicator** at the bottom left. |
| 158 | +::: |
0 commit comments