@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
142 lines (140 loc) • 5.54 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
function _callSuper(_this, derived, args) {
function isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
return !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
} catch (e) {
return false;
}
}
derived = _getPrototypeOf(derived);
return _possibleConstructorReturn(_this, isNativeReflectConstruct() ? Reflect.construct(derived, args || [], _getPrototypeOf(_this).constructor) : derived.apply(_this, args));
}
import React, { Component } from 'react';
import Canvas from '../Canvas';
import canvasPropTypes, { pickProps, defaultProps } from '../Canvas/propTypes';
var Polygon = /*#__PURE__*/function (_Component) {
function Polygon() {
var _this2;
_classCallCheck(this, Polygon);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this2 = _callSuper(this, Polygon, [].concat(args));
_defineProperty(_this2, "canvasRef", function (node) {
_this2.canvas = node;
});
_defineProperty(_this2, "drawShape", function (coordinates) {
if (coordinates.length === 0) return;
var ctx = _this2.canvas.canvasElement.getContext('2d');
_this2.canvas.setStrokeStyle();
coordinates.forEach(function (item, index, array) {
var nextCoordinate = array[index + 1];
if (nextCoordinate !== void 0) {
var initialCoord = null;
if (index === 0) {
initialCoord = item;
}
_this2.__drawLine(initialCoord, nextCoordinate);
}
});
_this2.canvas.setFillStyle();
ctx.fill();
});
_defineProperty(_this2, "onMouseMove", function (e) {
if (_this2.canvas.isDrawing()) {
_this2.clearCanvas();
var currentCoordinate = _this2.canvas.getCurrentCoordinates(e);
var lines = _this2.props.coordinates.concat([currentCoordinate]); // add temporary coordinate
// add initial coordinate at the end of the array to close the polygon
if (lines.length > 2) {
lines = lines.concat([lines[0]]);
}
_this2.drawShape(lines);
}
});
_defineProperty(_this2, "onMouseOut", function (e) {
if (_this2.canvas.isDrawing()) {
var initialCoordinate = _this2.canvas.getInitialCoordinate();
var newCoordinates = _this2.props.coordinates.concat([initialCoordinate]);
if (newCoordinates.length > 3) {
_this2.clearCanvas();
_this2.drawShape(newCoordinates);
_this2.canvas.stopDrawing(newCoordinates);
}
}
});
_defineProperty(_this2, "onDoubleClick", function () {
_this2.clearCanvas();
var initialCoordinate = _this2.canvas.getInitialCoordinate();
var newCoordinates = _this2.props.coordinates.concat([initialCoordinate]);
_this2.drawShape(newCoordinates);
if (_this2.canvas.isDrawing()) {
_this2.canvas.stopDrawing(newCoordinates);
}
});
_defineProperty(_this2, "onClick", function (e) {
if (_this2.canvas.isDrawing()) {
_this2.clearCanvas();
var currentCoordinate = _this2.canvas.getCurrentCoordinates(e);
var lastCoordinate = _this2.canvas.getLastCoordinate();
if (currentCoordinate.x === lastCoordinate.x && currentCoordinate.y === lastCoordinate.y) {
return; // in case the user click at the same point, this click is discarded
}
var newCoordinates = _this2.props.coordinates.concat([currentCoordinate]);
_this2.drawShape(newCoordinates);
_this2.props.handleSetCoordinates(newCoordinates, true);
} else {
_this2.canvas.startDrawing(e);
}
});
return _this2;
}
_inherits(Polygon, _Component);
return _createClass(Polygon, [{
key: "__drawLine",
value: function __drawLine(begin, end) {
var ctx = this.canvas.canvasElement.getContext('2d');
if (begin) {
ctx.beginPath();
ctx.moveTo(begin.x, begin.y);
}
ctx.lineTo(end.x, end.y);
ctx.stroke();
}
// ===========
// ACTIONS
// ===========
}, {
key: "clearCanvas",
value: function clearCanvas() {
var ctx = this.canvas.canvasElement.getContext('2d');
ctx.clearRect(0, 0, this.canvas.canvasElement.width, this.canvas.canvasElement.height);
ctx.beginPath();
ctx.closePath();
}
}, {
key: "render",
value: function render() {
return /*#__PURE__*/React.createElement(Canvas, Object.assign({
realRef: this.canvasRef,
drawShape: this.drawShape,
onMouseMove: this.onMouseMove,
onMouseOut: this.onMouseOut,
onDoubleClick: this.onDoubleClick,
onClick: this.onClick
}, pickProps(this.props)));
}
}]);
}(Component);
_defineProperty(Polygon, "propTypes", canvasPropTypes);
_defineProperty(Polygon, "defaultProps", defaultProps);
export { Polygon as default };