UNPKG

waveform-playlist-nartj

Version:

Multiple track web audio editor and player with waveform preview

118 lines (97 loc) 2.97 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _fadeMaker = require("fade-maker"); var _fadeCurves = require("fade-curves"); /* * virtual-dom hook for drawing the fade curve to the canvas element. */ var FadeCanvasHook = /*#__PURE__*/function () { function FadeCanvasHook(type, shape, duration, samplesPerPixel) { (0, _classCallCheck2["default"])(this, FadeCanvasHook); this.type = type; this.shape = shape; this.duration = duration; this.samplesPerPixel = samplesPerPixel; } (0, _createClass2["default"])(FadeCanvasHook, [{ key: "hook", value: function hook(canvas, prop, prev) { // node is up to date. if (prev !== undefined && prev.shape === this.shape && prev.type === this.type && prev.duration === this.duration && prev.samplesPerPixel === this.samplesPerPixel) { return; } var ctx = canvas.getContext('2d'); var width = canvas.width; var height = canvas.height; var curve = FadeCanvasHook.createCurve(this.shape, this.type, width); var len = curve.length; var y = height - curve[0] * height; ctx.strokeStyle = 'black'; ctx.beginPath(); ctx.moveTo(0, y); for (var i = 1; i < len; i += 1) { y = height - curve[i] * height; ctx.lineTo(i, y); } ctx.stroke(); } }], [{ key: "createCurve", value: function createCurve(shape, type, width) { var reflection; var curve; switch (type) { case _fadeMaker.FADEIN: { reflection = 1; break; } case _fadeMaker.FADEOUT: { reflection = -1; break; } default: { throw new Error('Unsupported fade type.'); } } switch (shape) { case _fadeMaker.SCURVE: { curve = (0, _fadeCurves.sCurve)(width, reflection); break; } case _fadeMaker.LINEAR: { curve = (0, _fadeCurves.linear)(width, reflection); break; } case _fadeMaker.EXPONENTIAL: { curve = (0, _fadeCurves.exponential)(width, reflection); break; } case _fadeMaker.LOGARITHMIC: { curve = (0, _fadeCurves.logarithmic)(width, 10, reflection); break; } default: { throw new Error('Unsupported fade shape'); } } return curve; } }]); return FadeCanvasHook; }(); var _default = FadeCanvasHook; exports["default"] = _default;