Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/PainterBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface PainterBase {

// constructor(dom: HTMLElement, storage: Storage, opts: PainterOption, id: number): void

resize(width?: number | string, height?: number | string): void
resize(width?: number | string, height?: number | string, devicePixelRatio?: number): void
refresh(): void
clear(): void

Expand Down
24 changes: 22 additions & 2 deletions src/canvas/Layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,28 @@ export default class Layer extends Eventful {
return (this._paintRects || []).slice();
}

resize(width: number, height: number) {
const dpr = this.dpr;
/**
* Update dpr and keep context markers in sync.
* Returns true if dpr changed.
*/
updateDpr(dpr?: number): boolean {
if (dpr != null && dpr !== this.dpr) {
this.dpr = dpr;
// Keep a custom dpr marker in sync for downstream brush logic.
if (this.ctx) {
(this.ctx as ZRCanvasRenderingContext).dpr = dpr;
}
if (this.ctxBack) {
(this.ctxBack as ZRCanvasRenderingContext).dpr = dpr;
}
return true;
}
return false;
}

resize(width: number, height: number, dpr?: number) {
this.updateDpr(dpr);
dpr = this.dpr;

const dom = this.dom;
const domStyle = dom.style;
Expand Down
23 changes: 18 additions & 5 deletions src/canvas/Painter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {devicePixelRatio} from '../config';
import { devicePixelRatio, getDevicePixelRatio } from '../config';
import * as util from '../core/util';
import Layer, { LayerConfig } from './Layer';
import requestAnimationFrame from '../animation/requestAnimationFrame';
Expand Down Expand Up @@ -835,11 +835,24 @@ export default class CanvasPainter implements PainterBase {

/**
* 区域大小变化后重绘
* @param width 宽度
* @param height 高度
* @param devicePixelRatio 设备像素比,如果未指定,则自动获取当前设备像素比
*/
resize(
width?: number | string,
height?: number | string
height?: number | string,
devicePixelRatio?: number
) {
const newDpr = devicePixelRatio != null
? devicePixelRatio
: getDevicePixelRatio();

const dprChanged = newDpr !== this.dpr;
if (dprChanged) {
this.dpr = newDpr;
}

if (!this._domRoot.style) { // Maybe in node or worker
if (width == null || height == null) {
return;
Expand All @@ -848,7 +861,7 @@ export default class CanvasPainter implements PainterBase {
this._width = width as number;
this._height = height as number;

this.getLayer(CANVAS_ZLEVEL).resize(width as number, height as number);
this.getLayer(CANVAS_ZLEVEL).resize(width as number, height as number, this.dpr);
}
else {
const domRoot = this._domRoot;
Expand All @@ -867,13 +880,13 @@ export default class CanvasPainter implements PainterBase {
domRoot.style.display = '';

// 优化没有实际改变的resize
if (this._width !== width || height !== this._height) {
if (this._width !== width || height !== this._height || dprChanged) {
domRoot.style.width = width + 'px';
domRoot.style.height = height + 'px';

for (let id in this._layers) {
if (this._layers.hasOwnProperty(id)) {
this._layers[id].resize(width, height);
this._layers[id].resize(width, height, this.dpr);
}
}

Expand Down
22 changes: 12 additions & 10 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import env from './core/env';

let dpr = 1;

// If in browser environment
if (env.hasGlobalWindow) {
dpr = Math.max(
window.devicePixelRatio
|| (window.screen && (window.screen as any).deviceXDPI / (window.screen as any).logicalXDPI)
|| 1, 1
);
export function getDevicePixelRatio(): number {
let dpr = 1;

// If in browser environment
if (env.hasGlobalWindow) {
dpr = window.devicePixelRatio
|| (window.screen && (window.screen as any).deviceXDPI / (window.screen as any).logicalXDPI)
|| 1;
}

return dpr;
}

/**
Expand All @@ -19,7 +21,7 @@ if (env.hasGlobalWindow) {
export const debugMode = 0;

// retina 屏幕优化
export const devicePixelRatio = dpr;
export const devicePixelRatio = getDevicePixelRatio();


/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/PathProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import * as vec2 from './vector';
import BoundingRect from './BoundingRect';
import {devicePixelRatio as dpr} from '../config';
import { fromLine, fromCubic, fromQuadratic, fromArc } from './bbox';
import { cubicLength, cubicSubdivide, quadraticLength, quadraticSubdivide } from './curve';

Expand Down Expand Up @@ -177,6 +176,7 @@ export default class PathProxy {
// Compat. Previously there is no segmentIgnoreThreshold.
segmentIgnoreThreshold = segmentIgnoreThreshold || 0;
if (segmentIgnoreThreshold > 0) {
const dpr = this.dpr || 1;
this._ux = mathAbs(segmentIgnoreThreshold / dpr / sx) || 0;
this._uy = mathAbs(segmentIgnoreThreshold / dpr / sy) || 0;
}
Expand Down
3 changes: 2 additions & 1 deletion src/zrender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,13 @@ class ZRender {
resize(opts?: {
width?: number| string
height?: number | string
devicePixelRatio?: number
}) {
if (this._disposed) {
return;
}
opts = opts || {};
this.painter.resize(opts.width, opts.height);
this.painter.resize(opts.width, opts.height, opts.devicePixelRatio);
this.handler.resize();
}

Expand Down