html2canvas
Version:
Screenshots with JavaScript
122 lines (94 loc) • 5.69 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.reformatInputBounds = exports.inlineSelectElement = exports.inlineTextAreaElement = exports.inlineInputElement = exports.getInputBorderRadius = exports.INPUT_BACKGROUND = exports.INPUT_BORDERS = exports.INPUT_COLOR = undefined;
var _TextContainer = require('./TextContainer');
var _TextContainer2 = _interopRequireDefault(_TextContainer);
var _background = require('./parsing/background');
var _border = require('./parsing/border');
var _Circle = require('./drawing/Circle');
var _Circle2 = _interopRequireDefault(_Circle);
var _Vector = require('./drawing/Vector');
var _Vector2 = _interopRequireDefault(_Vector);
var _Color = require('./Color');
var _Color2 = _interopRequireDefault(_Color);
var _Length = require('./Length');
var _Length2 = _interopRequireDefault(_Length);
var _Bounds = require('./Bounds');
var _TextBounds = require('./TextBounds');
var _Util = require('./Util');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var INPUT_COLOR = exports.INPUT_COLOR = new _Color2.default([42, 42, 42]);
var INPUT_BORDER_COLOR = new _Color2.default([165, 165, 165]);
var INPUT_BACKGROUND_COLOR = new _Color2.default([222, 222, 222]);
var INPUT_BORDER = {
borderWidth: 1,
borderColor: INPUT_BORDER_COLOR,
borderStyle: _border.BORDER_STYLE.SOLID
};
var INPUT_BORDERS = exports.INPUT_BORDERS = [INPUT_BORDER, INPUT_BORDER, INPUT_BORDER, INPUT_BORDER];
var INPUT_BACKGROUND = exports.INPUT_BACKGROUND = {
backgroundColor: INPUT_BACKGROUND_COLOR,
backgroundImage: [],
backgroundClip: _background.BACKGROUND_CLIP.PADDING_BOX,
backgroundOrigin: _background.BACKGROUND_ORIGIN.PADDING_BOX
};
var RADIO_BORDER_RADIUS = new _Length2.default('50%');
var RADIO_BORDER_RADIUS_TUPLE = [RADIO_BORDER_RADIUS, RADIO_BORDER_RADIUS];
var INPUT_RADIO_BORDER_RADIUS = [RADIO_BORDER_RADIUS_TUPLE, RADIO_BORDER_RADIUS_TUPLE, RADIO_BORDER_RADIUS_TUPLE, RADIO_BORDER_RADIUS_TUPLE];
var CHECKBOX_BORDER_RADIUS = new _Length2.default('3px');
var CHECKBOX_BORDER_RADIUS_TUPLE = [CHECKBOX_BORDER_RADIUS, CHECKBOX_BORDER_RADIUS];
var INPUT_CHECKBOX_BORDER_RADIUS = [CHECKBOX_BORDER_RADIUS_TUPLE, CHECKBOX_BORDER_RADIUS_TUPLE, CHECKBOX_BORDER_RADIUS_TUPLE, CHECKBOX_BORDER_RADIUS_TUPLE];
var getInputBorderRadius = exports.getInputBorderRadius = function getInputBorderRadius(node) {
return node.type === 'radio' ? INPUT_RADIO_BORDER_RADIUS : INPUT_CHECKBOX_BORDER_RADIUS;
};
var inlineInputElement = exports.inlineInputElement = function inlineInputElement(node, container) {
if (node.type === 'radio' || node.type === 'checkbox') {
if (node.checked) {
var size = Math.min(container.bounds.width, container.bounds.height);
container.childNodes.push(node.type === 'checkbox' ? [new _Vector2.default(container.bounds.left + size * 0.39363, container.bounds.top + size * 0.79), new _Vector2.default(container.bounds.left + size * 0.16, container.bounds.top + size * 0.5549), new _Vector2.default(container.bounds.left + size * 0.27347, container.bounds.top + size * 0.44071), new _Vector2.default(container.bounds.left + size * 0.39694, container.bounds.top + size * 0.5649), new _Vector2.default(container.bounds.left + size * 0.72983, container.bounds.top + size * 0.23), new _Vector2.default(container.bounds.left + size * 0.84, container.bounds.top + size * 0.34085), new _Vector2.default(container.bounds.left + size * 0.39363, container.bounds.top + size * 0.79)] : new _Circle2.default(container.bounds.left + size / 4, container.bounds.top + size / 4, size / 4));
}
} else {
inlineFormElement(getInputValue(node), node, container, false);
}
};
var inlineTextAreaElement = exports.inlineTextAreaElement = function inlineTextAreaElement(node, container) {
inlineFormElement(node.value, node, container, true);
};
var inlineSelectElement = exports.inlineSelectElement = function inlineSelectElement(node, container) {
var option = node.options[node.selectedIndex || 0];
inlineFormElement(option ? option.text || '' : '', node, container, false);
};
var reformatInputBounds = exports.reformatInputBounds = function reformatInputBounds(bounds) {
if (bounds.width > bounds.height) {
bounds.left += (bounds.width - bounds.height) / 2;
bounds.width = bounds.height;
} else if (bounds.width < bounds.height) {
bounds.top += (bounds.height - bounds.width) / 2;
bounds.height = bounds.width;
}
return bounds;
};
var inlineFormElement = function inlineFormElement(value, node, container, allowLinebreak) {
var body = node.ownerDocument.body;
if (value.length > 0 && body) {
var wrapper = node.ownerDocument.createElement('html2canvaswrapper');
(0, _Util.copyCSSStyles)(node.ownerDocument.defaultView.getComputedStyle(node, null), wrapper);
wrapper.style.position = 'absolute';
wrapper.style.left = container.bounds.left + 'px';
wrapper.style.top = container.bounds.top + 'px';
if (!allowLinebreak) {
wrapper.style.whiteSpace = 'nowrap';
}
var text = node.ownerDocument.createTextNode(value);
wrapper.appendChild(text);
body.appendChild(wrapper);
container.childNodes.push(_TextContainer2.default.fromTextNode(text, container));
body.removeChild(wrapper);
}
};
var getInputValue = function getInputValue(node) {
var value = node.type === 'password' ? new Array(node.value.length + 1).join('\u2022') : node.value;
return value.length === 0 ? node.placeholder || '' : value;
};