UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

205 lines (204 loc) • 7.95 kB
function _assert_this_initialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _call_super(_this, derived, args) { derived = _get_prototype_of(derived); return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args)); } function _class_call_check(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _create_class(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _define_property(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 _get_prototype_of(o) { _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _get_prototype_of(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _set_prototype_of(subClass, superClass); } function _object_spread(target) { for(var i = 1; i < arguments.length; i++){ var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === "function") { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function(key) { _define_property(target, key, source[key]); }); } return target; } function _possible_constructor_return(self, call) { if (call && (_type_of(call) === "object" || typeof call === "function")) { return call; } return _assert_this_initialized(self); } function _set_prototype_of(o, p) { _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _set_prototype_of(o, p); } function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } function _is_native_reflect_construct() { try { var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {})); } catch (_) {} return (_is_native_reflect_construct = function() { return !!result; })(); } import React, { Component } from 'react'; import '../ellipsePolyfill'; import Canvas from '../Canvas'; import canvasPropTypes, { pickProps, defaultProps } from '../Canvas/propTypes'; var Oval = /*#__PURE__*/ function(Component) { "use strict"; _inherits(Oval, Component); function Oval() { _class_call_check(this, Oval); var _this; _this = _call_super(this, Oval, arguments), _define_property(_this, "canvasRef", function(node) { _this.canvas = node; }), // this type needs exactly 2 coordinates to draw a ellipsis _define_property(_this, "drawShape", function() { var coordinates = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : []; if (coordinates.length === 2) { var ctx = _this.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; _this.canvas.setFillStyle(); // content ctx.beginPath(); ctx.ellipse(centerX, centerY, Math.abs(radiusX), Math.abs(radiusY), 0, 0, 2 * Math.PI); ctx.fill(); _this.canvas.setStrokeStyle(); // border ctx.stroke(); } }), // =========== // ACTIONS // =========== _define_property(_this, "onMouseMove", function(e) { if (_this.canvas.isDrawing()) { var coordinates = _this.__getOvalCoordinates(e); _this.canvas.drawShape(coordinates); } }), _define_property(_this, "onMouseDown", function(e) { if (!_this.canvas.isDrawing()) { _this.canvas.startDrawing(e); } }), _define_property(_this, "onMouseUp", function(e) { if (_this.canvas.isDrawing()) { var coordinates = _this.__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) { _this.canvas.stopDrawing(coordinates); } } }), _define_property(_this, "onTouchStart", function(e) { e.preventDefault(); var evt = _this.canvas.convertTouchCoordinates(e); _this.onMouseDown(evt); }), _define_property(_this, "onTouchMove", function(e) { var evt = _this.canvas.convertTouchCoordinates(e); _this.onMouseMove(evt); }), _define_property(_this, "onTouchEnd", function(e) { e.preventDefault(); var evt = _this.canvas.convertTouchCoordinates(e); _this.onMouseUp(evt); }); return _this; } _create_class(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_spread({ 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))); } } ]); return Oval; }(Component); _define_property(Oval, "propTypes", canvasPropTypes); _define_property(Oval, "defaultProps", defaultProps); export { Oval as default };