import * as React from "react"; // import {Image as KonvaImage, Group} from 'react-konva'; import {ReportItemDesigner} from "./ReportItemDesigner"; import {ImageItem} from "../../items/ImageItem"; import {RectF, SizeF} from "../../drawing/Unit"; import {autobind} from "core-decorators"; import convertHexToRGB = __MaterialUI.Utils.ColorManipulator.convertHexToRGB; import {ScaleImageEnum, HorizontalImageAlignEnum, VerticalImageAlignEnum} from "../../jasper/type/Enums"; import {PaddingDrawer} from "./decorates/PaddingDrawer"; import {GroupObjectPool} from "../../drawing/KonvaObjectPools"; const Konva = require('konva'); function autoResizeImage(maxWidth: number, maxHeight: number, img: {width: number, height: number}) { let hRatio; let wRatio; let ratio = 1; let w = img.width; let h = img.height; wRatio = maxWidth / w; hRatio = maxHeight / h; if (maxWidth == 0 && maxHeight == 0) { ratio = 1; } else if (maxWidth == 0) {// if (hRatio < 1) ratio = hRatio; } else if (maxHeight == 0) { if (wRatio < 1) ratio = wRatio; } else if (wRatio < 1 || hRatio < 1) { ratio = (wRatio <= hRatio ? wRatio : hRatio); } if (ratio < 1) { w = w * ratio; h = h * ratio; } img.height = h; img.width = w; } export class PictureBoxDesigner extends ReportItemDesigner { private clipWidth: number; private clipHeight: number; private imageObj; get pictureBox(): ImageItem { return this.item as ImageItem; } @autobind bindNode(node): void { if (node instanceof Konva.Image) { if (!this.imageObj || this.pictureBox.value !== this.imageObj.src) { if (this.pictureBox.value) { const imageObj = new Image(); imageObj.onload = () => { this.layoutImage(node, imageObj); try { node.parent.draw(); } catch (e) { } this.imageObj = imageObj; }; imageObj.src = this.pictureBox.value; } } else { const imageObj = this.imageObj; this.layoutImage(node, imageObj); } } } layoutImage(node, imageObj): void { let imageWidth = 0; let imageHeight = 0; let x = 0; let y = 0; const fillMode = this.pictureBox.scaleImage; const hAlign = this.pictureBox.horizontalImageAlign; const vAlign = this.pictureBox.verticalImageAlign; const zoom = this.reportDesigner.zoom; if (fillMode === ScaleImageEnum.Clip) { imageWidth = imageObj.width * zoom; imageHeight = imageObj.height * zoom; } else if (fillMode === ScaleImageEnum.FillFrame) { imageWidth = this.clipWidth; imageHeight = this.clipHeight; } else if (fillMode === ScaleImageEnum.RetainShape || fillMode === ScaleImageEnum.RealHeight || fillMode === ScaleImageEnum.RealSize) { const imageSize = {width: imageObj.width * zoom, height: imageObj.height * zoom}; autoResizeImage(this.clipWidth, this.clipHeight, imageSize); imageWidth = imageSize.width; imageHeight = imageSize.height; } if (hAlign === HorizontalImageAlignEnum.Center) { // 水平居中 x = (this.clipWidth - imageWidth) / 2.0; } if (vAlign === VerticalImageAlignEnum.Middle) { // 水平居中 y = (this.clipHeight - imageHeight) / 2.0; } node.x(x); node.y(y); node.width(imageWidth); node.height(imageHeight); node.image(imageObj); } // @BorderDrawer @PaddingDrawer render(bounds: RectF): any { const padding = { top: 0, left: 0, right: 0, bottom: 0 }; const clipWidth = bounds.width - padding.left - padding.right; const clipHeight = bounds.height - padding.top - padding.bottom; const rootProps = { x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height, clipX: padding.left, clipY: padding.top, clipWidth: clipWidth, clipHeight: clipHeight, }; this.clipWidth = clipWidth; this.clipHeight = clipHeight; const props = {}; const groupNode = GroupObjectPool.get(rootProps); const imageNode = new Konva.Image(props); groupNode.add(imageNode); this.bindNode(imageNode); return groupNode; } }