-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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 #1663 from tradingview/extract-watermark-into-plugin
Extract watermark into plugin
- Loading branch information
Showing
33 changed files
with
1,120 additions
and
494 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 was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export interface ImageWatermarkOptions { | ||
/** | ||
* Maximum width for the image watermark. | ||
* | ||
* @defaultValue undefined | ||
*/ | ||
maxWidth?: number; | ||
/** | ||
* Maximum height for the image watermark. | ||
* | ||
* @defaultValue undefined | ||
*/ | ||
maxHeight?: number; | ||
/** | ||
* Padding to maintain around the image watermark relative | ||
* to the chart pane edges. | ||
* | ||
* @defaultValue 0 | ||
*/ | ||
padding: number; | ||
/** | ||
* The alpha (opacity) for the image watermark. Where `1` is fully | ||
* opaque (visible) and `0` is fully transparent. | ||
* | ||
* @defaultValue 1 | ||
*/ | ||
alpha: number; | ||
} | ||
|
||
export const imageWatermarkOptionsDefaults: ImageWatermarkOptions = { | ||
alpha: 1, | ||
padding: 0, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
CanvasRenderingTarget2D, | ||
MediaCoordinatesRenderingScope, | ||
} from 'fancy-canvas'; | ||
|
||
import { IPrimitivePaneRenderer } from '../../model/ipane-primitive'; | ||
|
||
import { ImageWatermarkOptions } from './options'; | ||
|
||
export interface Placement { | ||
x: number; | ||
y: number; | ||
height: number; | ||
width: number; | ||
} | ||
|
||
export interface ImageWatermarkRendererOptions extends ImageWatermarkOptions { | ||
placement: Placement | null; | ||
imgElement: HTMLImageElement | null; | ||
} | ||
|
||
export class ImageWatermarkRenderer implements IPrimitivePaneRenderer { | ||
private _data: ImageWatermarkRendererOptions; | ||
|
||
public constructor(data: ImageWatermarkRendererOptions) { | ||
this._data = data; | ||
} | ||
|
||
public draw(target: CanvasRenderingTarget2D): void { | ||
target.useMediaCoordinateSpace((scope: MediaCoordinatesRenderingScope) => { | ||
const ctx = scope.context; | ||
const pos = this._data.placement; | ||
if (!pos) { | ||
return; | ||
} | ||
if (!this._data.imgElement) { | ||
throw new Error(`Image element missing.`); | ||
} | ||
ctx.globalAlpha = this._data.alpha ?? 1; | ||
ctx.drawImage(this._data.imgElement, pos.x, pos.y, pos.width, pos.height); | ||
}); | ||
} | ||
} |
Oops, something went wrong.