Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/js/ui/avatar-editor/src/canvas-tool/ |
Upload File : |
import {Text, Dom, Tag} from 'main.core'; import {EventEmitter} from "main.core.events"; import CanvasLoader from "./canvas-loader"; import {Options} from '../options'; type ActiveMaskType = { id: String, src: String }; export default class CanvasMask extends EventEmitter { #canvas: ?HTMLCanvasElement; #ctx: ?CanvasRenderingContext2D; #container: HTMLElement; #activeMask: ?ActiveMaskType = null; constructor(container) { super(); this.setEventNamespace('Main.Avatar.Editor'); this.#container = container; this.set = this.set.bind(this); this.mask = this.mask.bind(this); this.unmask = this.unmask.bind(this); } mask({id, src, thumb}) { Dom.style(this.#container, 'background-image', 'url("' + Text.encode(thumb) + '")'); this.#activeMask = {id, src}; CanvasLoader.loadFile( src, this.set, this.unmask ); } set(imageSource) { //region set image const trueK = Math.max( imageSource.width > 0 ? Options.maskSize / imageSource.width : 1, imageSource.height > 0 ? Options.maskSize / imageSource.height : 1 ); const sourceS = parseInt(Options.maskSize / trueK); const sourceX = parseInt((imageSource.width - sourceS) / 2); const sourceY = parseInt((imageSource.height - sourceS) / 2); this.getCanvas().width = Options.maskSize; this.getCanvas().height = Options.maskSize; this.getContext().clearRect(0, 0, Options.maskSize, Options.maskSize); this.getContext().drawImage(imageSource, sourceX, sourceY, sourceS, sourceS, 0, 0, Options.maskSize, Options.maskSize ); //endregion } applyAndPack(imageSource: HTMLCanvasElement): Promise { if (this.#activeMask === null || imageSource.width < 100 || imageSource.height < 100 ) { return Promise.reject({message: 'Image for masking has not appropriate format', code: 'bad image'}); } return new Promise((resolve) => { const trueK = Math.max( imageSource.width > 0 ? Options.maskSize / imageSource.width : 1, imageSource.height > 0 ? Options.maskSize / imageSource.height : 1 ); const sourceS = parseInt(Options.maskSize / trueK); const sourceX = parseInt((imageSource.width - sourceS) / 2); const sourceY = parseInt((imageSource.height - sourceS) / 2); const loader = CanvasLoader.getInstance(); loader.getCanvas().width = Options.maskSize; loader.getCanvas().height = Options.maskSize; loader.getContext().clearRect(0, 0, Options.maskSize, Options.maskSize); loader.getContext().drawImage(imageSource, sourceX, sourceY, sourceS, sourceS, 0, 0, Options.maskSize, Options.maskSize ); loader.getContext().drawImage( this.getCanvas(), 0, 0, Options.maskSize, Options.maskSize ); loader .pack('image/png') .then(blob => { blob.name = 'mask.png'; blob.maskId = this.#activeMask.id; resolve(blob, this.#activeMask.id); }) ; }); } getCanvas(): HTMLCanvasElement { if (!this.#canvas) { this.#canvas = document.createElement('CANVAS'); } return this.#canvas; } getContext(): CanvasRenderingContext2D { if (!this.#ctx) { this.#ctx = this.getCanvas().getContext("2d"); } return this.#ctx; } unmask() { this.#container.style.backgroundImage = ''; this.#activeMask = null; } }