fix:web stretching insets when original image exceeds img dimensions
This commit is contained in:
parent
067cd28bfb
commit
2029fc26c0
37
doric-web/dist/index.js
vendored
37
doric-web/dist/index.js
vendored
@ -7441,6 +7441,15 @@ var doric_web = (function (exports, axios, sandbox) {
|
|||||||
this.loadPlaceHolder(targetElement);
|
this.loadPlaceHolder(targetElement);
|
||||||
let tempLoadElement = this.stretchInset ? document.createElement('img') : targetElement;
|
let tempLoadElement = this.stretchInset ? document.createElement('img') : targetElement;
|
||||||
tempLoadElement.onload = () => {
|
tempLoadElement.onload = () => {
|
||||||
|
if (this.stretchInset) {
|
||||||
|
if (!this.resizeObserver) {
|
||||||
|
this.resizeObserver = new ResizeObserver(entry => {
|
||||||
|
this.onResize.call(this, { width: tempLoadElement.naturalWidth, height: tempLoadElement.naturalHeight });
|
||||||
|
});
|
||||||
|
this.resizeObserver.observe(targetElement);
|
||||||
|
}
|
||||||
|
this.onResize({ width: tempLoadElement.naturalWidth, height: tempLoadElement.naturalHeight });
|
||||||
|
}
|
||||||
//remove placeHolderColor
|
//remove placeHolderColor
|
||||||
targetElement.style.removeProperty('background-color');
|
targetElement.style.removeProperty('background-color');
|
||||||
if (tempLoadElement.src === src && this.onloadFuncId) {
|
if (tempLoadElement.src === src && this.onloadFuncId) {
|
||||||
@ -7475,11 +7484,16 @@ var doric_web = (function (exports, axios, sandbox) {
|
|||||||
v.src = transparentBase64;
|
v.src = transparentBase64;
|
||||||
v.style.borderImageSource = `url(${src})`;
|
v.style.borderImageSource = `url(${src})`;
|
||||||
v.style.borderImageSlice = `${stretchInset.top} ${stretchInset.right} ${stretchInset.bottom} ${stretchInset.left} fill`;
|
v.style.borderImageSlice = `${stretchInset.top} ${stretchInset.right} ${stretchInset.bottom} ${stretchInset.left} fill`;
|
||||||
const top = toPixelString(stretchInset.top);
|
}
|
||||||
const right = toPixelString(stretchInset.right);
|
calculateStretchBorderWidth(originalSize, targetSize, stretchInset) {
|
||||||
const bottom = toPixelString(stretchInset.bottom);
|
const widthRatio = targetSize.width / originalSize.width;
|
||||||
const left = toPixelString(stretchInset.left);
|
const heightRatio = targetSize.height / originalSize.height;
|
||||||
v.style.borderImageWidth = `${top} ${right} ${bottom} ${left}`;
|
const scaleFactor = Math.min(widthRatio, heightRatio);
|
||||||
|
const scaledStretchInset = {};
|
||||||
|
for (const key in stretchInset) {
|
||||||
|
scaledStretchInset[key] = Math.round(stretchInset[key] * scaleFactor);
|
||||||
|
}
|
||||||
|
return scaledStretchInset;
|
||||||
}
|
}
|
||||||
loadPlaceHolder(v) {
|
loadPlaceHolder(v) {
|
||||||
if (this.placeHolderImage) {
|
if (this.placeHolderImage) {
|
||||||
@ -7522,6 +7536,19 @@ var doric_web = (function (exports, axios, sandbox) {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
onResize(originalSize) {
|
||||||
|
if (!this.stretchInset) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this.view.offsetWidth !== 0 || this.view.offsetHeight !== 0) {
|
||||||
|
const scaledStretchInset = this.calculateStretchBorderWidth({ width: originalSize.width, height: originalSize.height }, { width: this.view.offsetWidth, height: this.view.offsetHeight }, this.stretchInset);
|
||||||
|
const top = toPixelString(scaledStretchInset.top);
|
||||||
|
const right = toPixelString(scaledStretchInset.right);
|
||||||
|
const bottom = toPixelString(scaledStretchInset.bottom);
|
||||||
|
const left = toPixelString(scaledStretchInset.left);
|
||||||
|
this.view.style.borderImageWidth = `${top} ${right} ${bottom} ${left}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DoricScrollerNode extends DoricSuperNode {
|
class DoricScrollerNode extends DoricSuperNode {
|
||||||
|
2
doric-web/dist/index.js.map
vendored
2
doric-web/dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
@ -1,6 +1,7 @@
|
|||||||
import { DoricViewNode, LEFT, RIGHT, CENTER_X, CENTER_Y, TOP, BOTTOM, toPixelString } from './DoricViewNode'
|
import { DoricViewNode, LEFT, RIGHT, CENTER_X, CENTER_Y, TOP, BOTTOM, toPixelString } from './DoricViewNode'
|
||||||
import { toRGBAString } from '../utils/color'
|
import { toRGBAString } from '../utils/color'
|
||||||
import { resourceManager } from '../DoricRegistry'
|
import { resourceManager } from '../DoricRegistry'
|
||||||
|
import Size from '../utils/Size'
|
||||||
|
|
||||||
enum ScaleType {
|
enum ScaleType {
|
||||||
ScaleToFill = 0,
|
ScaleToFill = 0,
|
||||||
@ -8,6 +9,7 @@ enum ScaleType {
|
|||||||
ScaleAspectFill,
|
ScaleAspectFill,
|
||||||
}
|
}
|
||||||
type StretchInset = {
|
type StretchInset = {
|
||||||
|
[key: string]: number;
|
||||||
left: number,
|
left: number,
|
||||||
top: number,
|
top: number,
|
||||||
right: number,
|
right: number,
|
||||||
@ -23,6 +25,7 @@ export class DoricImageNode extends DoricViewNode {
|
|||||||
errorImageBase64?: string
|
errorImageBase64?: string
|
||||||
errorColor?: number
|
errorColor?: number
|
||||||
stretchInset?: StretchInset
|
stretchInset?: StretchInset
|
||||||
|
resizeObserver?: ResizeObserver | null
|
||||||
|
|
||||||
build(): HTMLElement {
|
build(): HTMLElement {
|
||||||
const ret = document.createElement('img')
|
const ret = document.createElement('img')
|
||||||
@ -97,6 +100,15 @@ export class DoricImageNode extends DoricViewNode {
|
|||||||
this.loadPlaceHolder(targetElement)
|
this.loadPlaceHolder(targetElement)
|
||||||
let tempLoadElement = this.stretchInset ? document.createElement('img') : targetElement
|
let tempLoadElement = this.stretchInset ? document.createElement('img') : targetElement
|
||||||
tempLoadElement.onload = () => {
|
tempLoadElement.onload = () => {
|
||||||
|
if (this.stretchInset) {
|
||||||
|
if (!this.resizeObserver) {
|
||||||
|
this.resizeObserver = new ResizeObserver(entry => {
|
||||||
|
this.onResize.call(this, { width: tempLoadElement.naturalWidth, height: tempLoadElement.naturalHeight })
|
||||||
|
})
|
||||||
|
this.resizeObserver.observe(targetElement)
|
||||||
|
}
|
||||||
|
this.onResize({ width: tempLoadElement.naturalWidth, height: tempLoadElement.naturalHeight })
|
||||||
|
}
|
||||||
//remove placeHolderColor
|
//remove placeHolderColor
|
||||||
targetElement.style.removeProperty('background-color')
|
targetElement.style.removeProperty('background-color')
|
||||||
if (tempLoadElement.src === src && this.onloadFuncId) {
|
if (tempLoadElement.src === src && this.onloadFuncId) {
|
||||||
@ -130,11 +142,18 @@ export class DoricImageNode extends DoricViewNode {
|
|||||||
v.src = transparentBase64
|
v.src = transparentBase64
|
||||||
v.style.borderImageSource = `url(${src})`
|
v.style.borderImageSource = `url(${src})`
|
||||||
v.style.borderImageSlice = `${stretchInset.top} ${stretchInset.right} ${stretchInset.bottom} ${stretchInset.left} fill`
|
v.style.borderImageSlice = `${stretchInset.top} ${stretchInset.right} ${stretchInset.bottom} ${stretchInset.left} fill`
|
||||||
const top = toPixelString(stretchInset.top)
|
}
|
||||||
const right = toPixelString(stretchInset.right)
|
|
||||||
const bottom = toPixelString(stretchInset.bottom)
|
private calculateStretchBorderWidth(originalSize: Size, targetSize: Size, stretchInset: StretchInset) {
|
||||||
const left = toPixelString(stretchInset.left)
|
const widthRatio = targetSize.width / originalSize.width
|
||||||
v.style.borderImageWidth = `${top} ${right} ${bottom} ${left}`
|
const heightRatio = targetSize.height / originalSize.height
|
||||||
|
const scaleFactor = Math.min(widthRatio, heightRatio)
|
||||||
|
const scaledStretchInset: { [key: string]: number; } = {}
|
||||||
|
for (const key in stretchInset) {
|
||||||
|
scaledStretchInset[key] = Math.round(stretchInset[key] * scaleFactor)
|
||||||
|
}
|
||||||
|
return scaledStretchInset
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private loadPlaceHolder(v: HTMLImageElement) {
|
private loadPlaceHolder(v: HTMLImageElement) {
|
||||||
@ -182,4 +201,21 @@ export class DoricImageNode extends DoricViewNode {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private onResize(originalSize: Size) {
|
||||||
|
if (!this.stretchInset) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (this.view.offsetWidth !== 0 || this.view.offsetHeight !== 0) {
|
||||||
|
const scaledStretchInset = this.calculateStretchBorderWidth(
|
||||||
|
{ width: originalSize.width, height: originalSize.height },
|
||||||
|
{ width: this.view.offsetWidth, height: this.view.offsetHeight },
|
||||||
|
this.stretchInset)
|
||||||
|
const top = toPixelString(scaledStretchInset.top)
|
||||||
|
const right = toPixelString(scaledStretchInset.right)
|
||||||
|
const bottom = toPixelString(scaledStretchInset.bottom)
|
||||||
|
const left = toPixelString(scaledStretchInset.left)
|
||||||
|
this.view.style.borderImageWidth = `${top} ${right} ${bottom} ${left}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
4
doric-web/src/utils/Size.ts
Normal file
4
doric-web/src/utils/Size.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export default interface Size {
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
}
|
Reference in New Issue
Block a user