commit 1d93dc71393cf34079292d22b1db2005eac372ea
parent 87c151a4227bc3d4f090b7e7a29d430cb5288f66
Author: Massimo Siboldi <mdsiboldi@gmail.com>
Date: Sun, 10 Dec 2017 18:43:43 -0800
Add throttle to wave editor
Diffstat:
2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/src/WaveEditor/index.js b/src/WaveEditor/index.js
@@ -2,7 +2,7 @@ import { h, Component } from 'preact';
import consts from '../consts.js';
import helpers from '../helpers.js';
-const drawArea = (amplitudes, canvas, begin = 0, end = undefined) => {
+const drawArea = helpers.throttle((amplitudes, canvas, begin = 0, end = undefined) => {
const ctx = canvas.getContext("2d");
const rectWidth = canvas.width / consts.BUF_SIZE;
const roundedWidth = Math.max(1, rectWidth)
@@ -19,7 +19,7 @@ const drawArea = (amplitudes, canvas, begin = 0, end = undefined) => {
ctx.fillRect(roundedXOffset, -amp + halfCanvas, roundedWidth + 1, lineWidth);
});
});
-}
+}, 20)
const smoothZoneRange = function (waveData, begin, end) {
if (begin > end) {
@@ -38,7 +38,6 @@ export default class waveEditor extends Component {
componentDidMount() {
drawArea(this.props.waveform, this.canvasRef);
const handleMove = (ev) => {
- console.log('handling move');
this.updateWaveform({
x: ev.x + window.scrollX,
y: ev.y + window.scrollY
diff --git a/src/helpers.js b/src/helpers.js
@@ -9,6 +9,15 @@ export default {
handler(ev);
}
target.addEventListener(type, doOnce);
+ },
+ throttle: (fn, ms) => {
+ let time = 0;
+ return (...args) => {
+ let now = Date.now();
+ if (now >= time + ms) {
+ fn(...args);
+ time = now;
+ }
+ }
}
-
};