feat:redraw when device pixel ratio change

pr675
Hufe921 3 years ago
parent 189e88c560
commit 4c370aec1a

@ -5,8 +5,10 @@
"colspan", "colspan",
"compositionend", "compositionend",
"compositionstart", "compositionstart",
"contenteditable",
"contextmenu", "contextmenu",
"deletable", "deletable",
"dppx",
"inputarea", "inputarea",
"linebreak", "linebreak",
"prismjs", "prismjs",
@ -17,9 +19,8 @@
"TEXTLIKE", "TEXTLIKE",
"trlist", "trlist",
"vite", "vite",
"Yahei",
"vitepress", "vitepress",
"contenteditable" "Yahei"
], ],
"cSpell.ignorePaths": [ "cSpell.ignorePaths": [
".github", ".github",

@ -463,16 +463,21 @@ export class Draw {
} }
public setPageScale(payload: number) { public setPageScale(payload: number) {
const dpr = window.devicePixelRatio
this.options.scale = payload this.options.scale = payload
const width = this.getWidth() const width = this.getWidth()
const height = this.getHeight() const height = this.getHeight()
this.container.style.width = `${width}px` this.container.style.width = `${width}px`
this.pageList.forEach(p => { this.pageList.forEach((p, i) => {
p.width = width p.width = width
p.height = height p.height = height
p.style.width = `${width}px` p.style.width = `${width}px`
p.style.height = `${height}px` p.style.height = `${height}px`
p.style.marginBottom = `${this.getPageGap()}px` p.style.marginBottom = `${this.getPageGap()}px`
// 分辨率
p.width = width * dpr
p.height = height * dpr
this.ctxList[i].scale(dpr, dpr)
}) })
this.render({ this.render({
isSubmitHistory: false, isSubmitHistory: false,
@ -483,6 +488,21 @@ export class Draw {
} }
} }
public setPageDevicePixel() {
const dpr = window.devicePixelRatio
const width = this.getWidth()
const height = this.getHeight()
this.pageList.forEach((p, i) => {
p.width = width * dpr
p.height = height * dpr
this.ctxList[i].scale(dpr, dpr)
})
this.render({
isSubmitHistory: false,
isSetCursor: false
})
}
public setPaperSize(width: number, height: number) { public setPaperSize(width: number, height: number) {
this.options.width = width this.options.width = width
this.options.height = height this.options.height = height

@ -24,6 +24,7 @@ export class GlobalEvent {
private hyperlinkParticle: HyperlinkParticle private hyperlinkParticle: HyperlinkParticle
private control: Control private control: Control
private dateParticle: DateParticle private dateParticle: DateParticle
private dprMediaQueryList: MediaQueryList
constructor(draw: Draw, canvasEvent: CanvasEvent) { constructor(draw: Draw, canvasEvent: CanvasEvent) {
this.draw = draw this.draw = draw
@ -37,6 +38,7 @@ export class GlobalEvent {
this.hyperlinkParticle = draw.getHyperlinkParticle() this.hyperlinkParticle = draw.getHyperlinkParticle()
this.dateParticle = draw.getDateParticle() this.dateParticle = draw.getDateParticle()
this.control = draw.getControl() this.control = draw.getControl()
this.dprMediaQueryList = window.matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`)
} }
public register() { public register() {
@ -51,6 +53,7 @@ export class GlobalEvent {
document.addEventListener('mouseup', this.setCanvasEventAbility) document.addEventListener('mouseup', this.setCanvasEventAbility)
document.addEventListener('wheel', this.setPageScale, { passive: false }) document.addEventListener('wheel', this.setPageScale, { passive: false })
document.addEventListener('visibilitychange', this._handleVisibilityChange) document.addEventListener('visibilitychange', this._handleVisibilityChange)
this.dprMediaQueryList.addEventListener('change', this._handleDprChange)
} }
public removeEvent() { public removeEvent() {
@ -60,6 +63,7 @@ export class GlobalEvent {
document.removeEventListener('mouseup', this.setCanvasEventAbility) document.removeEventListener('mouseup', this.setCanvasEventAbility)
document.removeEventListener('wheel', this.setPageScale) document.removeEventListener('wheel', this.setPageScale)
document.removeEventListener('visibilitychange', this._handleVisibilityChange) document.removeEventListener('visibilitychange', this._handleVisibilityChange)
this.dprMediaQueryList.removeEventListener('change', this._handleDprChange)
} }
public recoverEffect = (evt: Event) => { public recoverEffect = (evt: Event) => {
@ -124,4 +128,8 @@ export class GlobalEvent {
} }
} }
private _handleDprChange = () => {
this.draw.setPageDevicePixel()
}
} }
Loading…
Cancel
Save