UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

127 lines (126 loc) 4.83 kB
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 '../ellipsePolyfill'; import Canvas from '../Canvas'; import canvasPropTypes, { pickProps, defaultProps } from '../Canvas/propTypes'; var Oval = /*#__PURE__*/function (_Component) { function Oval() { var _this2; _classCallCheck(this, Oval); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this2 = _callSuper(this, Oval, [].concat(args)); _defineProperty(_this2, "canvasRef", function (node) { _this2.canvas = node; }); // this type needs exactly 2 coordinates to draw a ellipsis _defineProperty(_this2, "drawShape", function () { var coordinates = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; if (coordinates.length === 2) { var ctx = _this2.canvas.canvasElement.getContext('2d'); var pointA = coordinates[0]; var pointB = coordinates[1]; var radiusX = (pointB.x - pointA.x) * 0.5; var radiusY = (pointB.y - pointA.y) * 0.5; var centerX = pointA.x + radiusX; var centerY = pointA.y + radiusY; _this2.canvas.setFillStyle(); // content ctx.beginPath(); ctx.ellipse(centerX, centerY, Math.abs(radiusX), Math.abs(radiusY), 0, 0, 2 * Math.PI); ctx.fill(); _this2.canvas.setStrokeStyle(); // border ctx.stroke(); } }); // =========== // ACTIONS // =========== _defineProperty(_this2, "onMouseMove", function (e) { if (_this2.canvas.isDrawing()) { var coordinates = _this2.__getOvalCoordinates(e); _this2.canvas.drawShape(coordinates); } }); _defineProperty(_this2, "onMouseDown", function (e) { if (!_this2.canvas.isDrawing()) { _this2.canvas.startDrawing(e); } }); _defineProperty(_this2, "onMouseUp", function (e) { if (_this2.canvas.isDrawing()) { var coordinates = _this2.__getOvalCoordinates(e); var c1 = coordinates[0]; var c2 = coordinates[1]; // if the release is not at the same position if (c1.x !== c2.x || c1.y !== c2.y) { _this2.canvas.stopDrawing(coordinates); } } }); _defineProperty(_this2, "onTouchStart", function (e) { e.preventDefault(); var evt = _this2.canvas.convertTouchCoordinates(e); _this2.onMouseDown(evt); }); _defineProperty(_this2, "onTouchMove", function (e) { var evt = _this2.canvas.convertTouchCoordinates(e); _this2.onMouseMove(evt); }); _defineProperty(_this2, "onTouchEnd", function (e) { e.preventDefault(); var evt = _this2.canvas.convertTouchCoordinates(e); _this2.onMouseUp(evt); }); return _this2; } _inherits(Oval, _Component); return _createClass(Oval, [{ key: "__getOvalCoordinates", value: function __getOvalCoordinates(e) { var startPoint = this.canvas.getInitialCoordinate(); var x2 = e.nativeEvent.offsetX; var y2 = e.nativeEvent.offsetY; return [startPoint, { x: x2, y: y2 }]; } }, { key: "render", value: function render() { return /*#__PURE__*/React.createElement(Canvas, Object.assign({ realRef: this.canvasRef, drawShape: this.drawShape, onMouseMove: this.onMouseMove, onMouseDown: this.onMouseDown, onMouseUp: this.onMouseUp, onTouchStart: this.onTouchStart, onTouchMove: this.onTouchMove, onTouchEnd: this.onTouchEnd }, pickProps(this.props))); } }]); }(Component); _defineProperty(Oval, "propTypes", canvasPropTypes); _defineProperty(Oval, "defaultProps", defaultProps); export { Oval as default };