Skip to content

Commit

Permalink
feat:redraw when device pixel ratio change
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Feb 19, 2023
1 parent 189e88c commit 4c370ae
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"colspan",
"compositionend",
"compositionstart",
"contenteditable",
"contextmenu",
"deletable",
"dppx",
"inputarea",
"linebreak",
"prismjs",
Expand All @@ -17,9 +19,8 @@
"TEXTLIKE",
"trlist",
"vite",
"Yahei",
"vitepress",
"contenteditable"
"Yahei"
],
"cSpell.ignorePaths": [
".github",
Expand Down
22 changes: 21 additions & 1 deletion src/editor/core/draw/Draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,16 +463,21 @@ export class Draw {
}

public setPageScale(payload: number) {
const dpr = window.devicePixelRatio
this.options.scale = payload
const width = this.getWidth()
const height = this.getHeight()
this.container.style.width = `${width}px`
this.pageList.forEach(p => {
this.pageList.forEach((p, i) => {
p.width = width
p.height = height
p.style.width = `${width}px`
p.style.height = `${height}px`
p.style.marginBottom = `${this.getPageGap()}px`
// 分辨率
p.width = width * dpr
p.height = height * dpr
this.ctxList[i].scale(dpr, dpr)
})
this.render({
isSubmitHistory: false,
Expand All @@ -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) {
this.options.width = width
this.options.height = height
Expand Down
8 changes: 8 additions & 0 deletions src/editor/core/event/GlobalEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class GlobalEvent {
private hyperlinkParticle: HyperlinkParticle
private control: Control
private dateParticle: DateParticle
private dprMediaQueryList: MediaQueryList

constructor(draw: Draw, canvasEvent: CanvasEvent) {
this.draw = draw
Expand All @@ -37,6 +38,7 @@ export class GlobalEvent {
this.hyperlinkParticle = draw.getHyperlinkParticle()
this.dateParticle = draw.getDateParticle()
this.control = draw.getControl()
this.dprMediaQueryList = window.matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`)
}

public register() {
Expand All @@ -51,6 +53,7 @@ export class GlobalEvent {
document.addEventListener('mouseup', this.setCanvasEventAbility)
document.addEventListener('wheel', this.setPageScale, { passive: false })
document.addEventListener('visibilitychange', this._handleVisibilityChange)
this.dprMediaQueryList.addEventListener('change', this._handleDprChange)
}

public removeEvent() {
Expand All @@ -60,6 +63,7 @@ export class GlobalEvent {
document.removeEventListener('mouseup', this.setCanvasEventAbility)
document.removeEventListener('wheel', this.setPageScale)
document.removeEventListener('visibilitychange', this._handleVisibilityChange)
this.dprMediaQueryList.removeEventListener('change', this._handleDprChange)
}

public recoverEffect = (evt: Event) => {
Expand Down Expand Up @@ -124,4 +128,8 @@ export class GlobalEvent {
}
}

private _handleDprChange = () => {
this.draw.setPageDevicePixel()
}

}

0 comments on commit 4c370ae

Please sign in to comment.