From c762f35fdef552df15a10b0a29df3e30e2c585e2 Mon Sep 17 00:00:00 2001 From: wryk Date: Fri, 15 Mar 2019 19:07:26 +0100 Subject: [PATCH] refactor(configuration): centralize constants --- src/constants.js | 4 ++++ src/services/capture.js | 22 ++++++++++++---------- src/services/encode.js | 4 ++-- 3 files changed, 18 insertions(+), 12 deletions(-) create mode 100644 src/constants.js diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 0000000..aaafd49 --- /dev/null +++ b/src/constants.js @@ -0,0 +1,4 @@ +export const GIF_WIDTH = 200 +export const GIF_HEIGHT = GIF_WIDTH +export const GIF_FRAME_RATE = 10 +export const GIF_PALETTE_SIZE = 255 \ No newline at end of file diff --git a/src/services/capture.js b/src/services/capture.js index c13ae9a..51e550d 100644 --- a/src/services/capture.js +++ b/src/services/capture.js @@ -1,12 +1,14 @@ +import { + GIF_WIDTH, + GIF_HEIGHT, + GIF_FRAME_RATE +} from '/constants.js' + import { makeRectangle, crop } from '/services/rectangle.js' -const FRAMES_PER_SECOND = 10 -const WIDTH = 200 -const HEIGHT = WIDTH - export function capture (commit, mediaStream, duration) { return new Promise((resolve, reject) => { const video = document.createElement('video') @@ -15,18 +17,18 @@ export function capture (commit, mediaStream, duration) { video.setAttribute('webkit-playsinline', '') const canvas = document.createElement('canvas') - canvas.width = WIDTH - canvas.height = HEIGHT + canvas.width = GIF_WIDTH + canvas.height = GIF_HEIGHT const canvasContext = canvas.getContext('2d') - const totalFrames = duration / 1000 * FRAMES_PER_SECOND + const totalFrames = duration / 1000 * GIF_FRAME_RATE if (totalFrames < 1) { resolve([]) } - const delayTime = 1000 / FRAMES_PER_SECOND + const delayTime = 1000 / GIF_FRAME_RATE video.srcObject = mediaStream @@ -67,8 +69,8 @@ export function capture (commit, mediaStream, duration) { resolve({ imageDataList, - imageWidth: WIDTH, - imageHeight: HEIGHT, + imageWidth: GIF_WIDTH, + imageHeight: GIF_HEIGHT, delayTime }) } diff --git a/src/services/encode.js b/src/services/encode.js index adaa84b..9b04a3f 100644 --- a/src/services/encode.js +++ b/src/services/encode.js @@ -4,7 +4,7 @@ import pEvent from 'p-event' import { write } from '/services/encode-core.js' import { promisesProgress, calcProgress } from '/services/util.js' -const PALETTE_SIZE = 255 +import { GIF_PALETTE_SIZE } from '/constants.js' export function encode ({ imageDataList, imageWidth, imageHeight, delayTime }) { const emitter = new EventEmitter() @@ -31,7 +31,7 @@ export function encode ({ imageDataList, imageWidth, imageHeight, delayTime }) { worker.postMessage({ imageData, - paletteSize: PALETTE_SIZE + paletteSize: GIF_PALETTE_SIZE }) })