-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #818 from tradingview/fancy-canvas-0.3
fancy-canvas 2
- Loading branch information
Showing
56 changed files
with
1,055 additions
and
927 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,26 @@ | ||
import { Binding as CanvasCoordinateSpaceBinding, bindToDevicePixelRatio } from 'fancy-canvas/coordinate-space'; | ||
import { | ||
bindCanvasElementBitmapSizeTo, | ||
CanvasElementBitmapSizeBinding, | ||
Size, | ||
} from 'fancy-canvas'; | ||
|
||
import { ensureNotNull } from '../helpers/assertions'; | ||
|
||
export class Size { | ||
public h: number; | ||
public w: number; | ||
|
||
public constructor(w: number, h: number) { | ||
this.w = w; | ||
this.h = h; | ||
} | ||
|
||
public equals(size: Size): boolean { | ||
return (this.w === size.w) && (this.h === size.h); | ||
} | ||
} | ||
|
||
export function getCanvasDevicePixelRatio(canvas: HTMLCanvasElement): number { | ||
return canvas.ownerDocument && | ||
canvas.ownerDocument.defaultView && | ||
canvas.ownerDocument.defaultView.devicePixelRatio | ||
|| 1; | ||
} | ||
|
||
export function getContext2D(canvas: HTMLCanvasElement): CanvasRenderingContext2D { | ||
const ctx = ensureNotNull(canvas.getContext('2d')); | ||
// sometimes (very often) ctx getContext returns the same context every time | ||
// and there might be previous transformation | ||
// so let's reset it to be sure that everything is ok | ||
// do no use resetTransform to respect Edge | ||
ctx.setTransform(1, 0, 0, 1, 0, 0); | ||
return ctx; | ||
} | ||
|
||
export function createPreconfiguredCanvas(doc: Document, size: Size): HTMLCanvasElement { | ||
const canvas = doc.createElement('canvas'); | ||
|
||
const pixelRatio = getCanvasDevicePixelRatio(canvas); | ||
// we should keep the layout size... | ||
canvas.style.width = `${size.w}px`; | ||
canvas.style.height = `${size.h}px`; | ||
// ...but multiply coordinate space dimensions to device pixel ratio | ||
canvas.width = size.w * pixelRatio; | ||
canvas.height = size.h * pixelRatio; | ||
return canvas; | ||
} | ||
|
||
export function createBoundCanvas(parentElement: HTMLElement, size: Size): CanvasCoordinateSpaceBinding { | ||
export function createBoundCanvas(parentElement: HTMLElement, size: Size): CanvasElementBitmapSizeBinding { | ||
const doc = ensureNotNull(parentElement.ownerDocument); | ||
const canvas = doc.createElement('canvas'); | ||
parentElement.appendChild(canvas); | ||
|
||
const binding = bindToDevicePixelRatio(canvas, { allowDownsampling: false }); | ||
binding.resizeCanvas({ | ||
width: size.w, | ||
height: size.h, | ||
const binding = bindCanvasElementBitmapSizeTo(canvas, { | ||
type: 'device-pixel-content-box', | ||
options: { | ||
allowResizeObserver: false, | ||
}, | ||
transform: (bitmapSize: Size, canvasElementClientSize: Size) => ({ | ||
width: Math.max(bitmapSize.width, canvasElementClientSize.width), | ||
height: Math.max(bitmapSize.height, canvasElementClientSize.height), | ||
}), | ||
}); | ||
binding.resizeCanvasElement(size); | ||
return binding; | ||
} | ||
|
||
export function drawScaled(ctx: CanvasRenderingContext2D, ratio: number, func: () => void): void { | ||
ctx.save(); | ||
ctx.scale(ratio, ratio); | ||
func(); | ||
ctx.restore(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.