ami-cjs.js
Version:
<p align="center"> <img src="https://cloud.githubusercontent.com/assets/214063/23213764/78ade038-f90c-11e6-8208-4fcade5f3832.png" width="60%"> </p>
246 lines (216 loc) • 8.82 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* @module helpers/lut
*/
var HelpersLut = function () {
function HelpersLut(containerID) {
var lut = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
var lutO = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'linear';
var color = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [[0, 0, 0, 0], [1, 1, 1, 1]];
var opacity = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [[0, 0], [1, 1]];
var discrete = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false;
_classCallCheck(this, HelpersLut);
// min/max (0-1 or real intensities)
// show/hide
// horizontal/vertical
this._containerID = containerID;
this._discrete = discrete;
this._color = color;
this._lut = lut;
this._luts = _defineProperty({}, lut, color);
this._opacity = opacity;
this._lutO = lutO;
this._lutsO = _defineProperty({}, lutO, opacity);
this.initCanvas();
this.paintCanvas();
}
_createClass(HelpersLut, [{
key: 'initCanvas',
value: function initCanvas() {
// container
this._canvasContainer = this.initCanvasContainer(this._containerID);
// background
this._canvasBg = this.createCanvas();
this._canvasContainer.appendChild(this._canvasBg);
// foreground
this._canvas = this.createCanvas();
this._canvasContainer.appendChild(this._canvas);
}
}, {
key: 'initCanvasContainer',
value: function initCanvasContainer(canvasContainerId) {
var canvasContainer = document.getElementById(canvasContainerId);
canvasContainer.style.width = '256 px';
canvasContainer.style.height = '128 px';
canvasContainer.style.border = '1px solid #F9F9F9';
return canvasContainer;
}
}, {
key: 'createCanvas',
value: function createCanvas() {
var canvas = document.createElement('canvas');
canvas.height = 16;
canvas.width = 256;
return canvas;
}
}, {
key: 'paintCanvas',
value: function paintCanvas() {
// setup context
var ctx = this._canvas.getContext('2d');
ctx.clearRect(0, 0, this._canvas.width, this._canvas.height);
ctx.globalCompositeOperation = 'source-over';
// apply color
if (!this._discrete) {
var color = ctx.createLinearGradient(0, 0, this._canvas.width, this._canvas.height);
for (var i = 0; i < this._color.length; i++) {
color.addColorStop(this._color[i][0], 'rgba( ' + Math.round(this._color[i][1] * 255) + ', ' + Math.round(this._color[i][2] * 255) + ', ' + Math.round(this._color[i][3] * 255) + ', 1)');
}
ctx.fillStyle = color;
ctx.fillRect(0, 0, this._canvas.width, this._canvas.height);
} else {
ctx.lineWidth = 2 * this._canvas.height;
for (var _i = 0; _i < this._color.length; _i++) {
var currentPos = this._color[_i][0];
var nextPos = 1;
if (_i < this._color.length - 1) {
nextPos = this._color[_i + 1][0];
}
var previousPos = 0;
if (_i > 0) {
previousPos = this._color[_i - 1][0];
}
var from = previousPos + (currentPos - previousPos) / 2;
var to = currentPos + (nextPos - currentPos) / 2;
var _color = this._color[_i];
var opacity = this._opacity[_i] ? this._opacity[_i][1] : 1;
ctx.beginPath();
ctx.strokeStyle = 'rgba( ' + Math.round(_color[1] * 255) + ', ' + Math.round(_color[2] * 255) + ', ' + Math.round(_color[3] * 255) + ', ' + opacity + ')';
ctx.moveTo(from * this._canvas.width, 0);
ctx.lineTo(to * this._canvas.width, 0);
ctx.stroke();
ctx.closePath();
}
}
if (!this._discrete) {
// if discrete, we already took care of the opacity.
// setup context
ctx.globalCompositeOperation = 'destination-in';
// apply opacity
var _opacity = ctx.createLinearGradient(0, 0, this._canvas.width, this._canvas.height);
for (var _i2 = 0; _i2 < this._opacity.length; _i2++) {
_opacity.addColorStop(this._opacity[_i2][0], 'rgba(255, 255, 255, ' + this._opacity[_i2][1] + ')');
}
ctx.fillStyle = _opacity;
ctx.fillRect(0, 0, this._canvas.width, this._canvas.height);
}
}
}, {
key: 'lutsAvailable',
value: function lutsAvailable() {
var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'color';
var available = [];
var luts = this._luts;
if (type !== 'color') {
luts = this._lutsO;
}
for (var i in luts) {
available.push(i);
}
return available;
}
// add luts to class' lut (so a user can add its own as well)
}, {
key: 'texture',
get: function get() {
var texture = new THREE.Texture(this._canvas);
texture.mapping = THREE.UVMapping;
texture.wrapS = texture.wrapT = THREE.ClampToEdgeWrapping;
texture.magFilter = texture.minFilter = THREE.NearestFilter;
texture.premultiplyAlpha = true;
texture.needsUpdate = true;
return texture;
}
}, {
key: 'lut',
set: function set(targetLUT) {
this._color = this._luts[targetLUT];
this._lut = targetLUT;
this.paintCanvas();
},
get: function get() {
return this._lut;
}
}, {
key: 'luts',
set: function set(newLuts) {
this._luts = newLuts;
},
get: function get() {
return this._luts;
}
}, {
key: 'lutO',
set: function set(targetLUTO) {
this._opacity = this._lutsO[targetLUTO];
this._lutO = targetLUTO;
this.paintCanvas();
},
get: function get() {
return this._lutO;
}
}, {
key: 'lutsO',
set: function set(newLutsO) {
this._lutsO = newLutsO;
},
get: function get() {
return this._lutsO;
}
}, {
key: 'discrete',
set: function set(discrete) {
this._discrete = discrete;
this.paintCanvas();
},
get: function get() {
return this._discrete;
}
}], [{
key: 'presetLuts',
value: function presetLuts() {
return {
'default': [[0, 0, 0, 0], [1, 1, 1, 1]],
'spectrum': [[0, 0, 0, 0], [0.1, 0, 0, 1], [0.33, 0, 1, 1], [0.5, 0, 1, 0], [0.66, 1, 1, 0], [0.9, 1, 0, 0], [1, 1, 1, 1]],
'hot_and_cold': [[0, 0, 0, 1], [0.15, 0, 1, 1], [0.3, 0, 1, 0], [0.45, 0, 0, 0], [0.5, 0, 0, 0], [0.55, 0, 0, 0], [0.7, 1, 1, 0], [0.85, 1, 0, 0], [1, 1, 1, 1]],
'gold': [[0, 0, 0, 0], [0.13, 0.19, 0.03, 0], [0.25, 0.39, 0.12, 0], [0.38, 0.59, 0.26, 0], [0.50, 0.80, 0.46, 0.08], [0.63, 0.99, 0.71, 0.21], [0.75, 0.99, 0.88, 0.34], [0.88, 0.99, 0.99, 0.48], [1, 0.90, 0.95, 0.61]],
'red': [[0, 0.75, 0, 0], [0.5, 1, 0.5, 0], [0.95, 1, 1, 0], [1, 1, 1, 1]],
'green': [[0, 0, 0.75, 0], [0.5, 0.5, 1, 0], [0.95, 1, 1, 0], [1, 1, 1, 1]],
'blue': [[0, 0, 0, 1], [0.5, 0, 0.5, 1], [0.95, 0, 1, 1], [1, 1, 1, 1]],
'walking_dead': [[0, 0.1, 1, 1], [1, 1, 1, 1]],
'random': [[0, 0, 0, 0], [0.27, 0.18, 0.18, 0.18], [0.41, 1, 1, 1], [0.7, 1, 0, 0], [1, 1, 1, 1]]
};
}
}, {
key: 'presetLutsO',
value: function presetLutsO() {
return {
'linear': [[0, 0], [1, 1]],
'lowpass': [[0, 0.8], [0.2, 0.6], [0.3, 0.1], [1, 0]],
'bandpass': [[0, 0], [0.4, 0.8], [0.6, 0.8], [1, 0]],
'highpass': [[0, 0], [0.7, 0.1], [0.8, 0.6], [1, 0.8]],
'flat': [[0, .7], [1, 1]],
'random': [[0, 0.], [0.38, 0.], [0.55, 1.], [0.72, 1.], [1, 0.05]]
};
}
}]);
return HelpersLut;
}();
exports.default = HelpersLut;
module.exports = exports['default'];