hyperprop-charting-library 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -51,7 +51,13 @@ var DEFAULT_WATERMARK_OPTIONS = {
51
51
  opacity: 0.14,
52
52
  fontSize: 92,
53
53
  fontWeight: 700,
54
- thickness: 0
54
+ thickness: 0,
55
+ imageSrc: "",
56
+ imageScale: 1,
57
+ imageMaxWidthRatio: 0.42,
58
+ imageMaxHeightRatio: 0.3,
59
+ imageTintColor: "",
60
+ imageTintOpacity: 1
55
61
  };
56
62
  var DEFAULT_PRICE_LINE_OPTIONS = {
57
63
  visible: true,
@@ -182,6 +188,9 @@ function createChart(element, options = {}) {
182
188
  let yMaxOverride = null;
183
189
  let autoYMin = null;
184
190
  let autoYMax = null;
191
+ let watermarkImageSrc = null;
192
+ let watermarkImage = null;
193
+ let watermarkImageReady = false;
185
194
  let drawState = null;
186
195
  let orderDragState = null;
187
196
  let actionDragState = null;
@@ -207,6 +216,29 @@ function createChart(element, options = {}) {
207
216
  }
208
217
  return Math.max(1, window.devicePixelRatio || 1);
209
218
  };
219
+ const ensureWatermarkImage = (src) => {
220
+ if (src === watermarkImageSrc) {
221
+ return;
222
+ }
223
+ watermarkImageSrc = src;
224
+ watermarkImageReady = false;
225
+ watermarkImage = null;
226
+ if (!src) {
227
+ return;
228
+ }
229
+ const image = new Image();
230
+ image.crossOrigin = "anonymous";
231
+ image.onload = () => {
232
+ watermarkImage = image;
233
+ watermarkImageReady = true;
234
+ draw();
235
+ };
236
+ image.onerror = () => {
237
+ watermarkImage = null;
238
+ watermarkImageReady = false;
239
+ };
240
+ image.src = src;
241
+ };
210
242
  const crisp = (value) => Math.round(value) + 0.5;
211
243
  const clamp = (value, min, max) => {
212
244
  return Math.min(max, Math.max(min, value));
@@ -755,6 +787,31 @@ function createChart(element, options = {}) {
755
787
  const yFromPrice = (price) => {
756
788
  return chartBottom - (price - yMin) / yRange * chartHeight;
757
789
  };
790
+ if (watermark.visible && watermark.imageSrc.trim().length > 0) {
791
+ ensureWatermarkImage(watermark.imageSrc.trim());
792
+ if (watermarkImageReady && watermarkImage) {
793
+ const maxW = chartWidth * clamp(watermark.imageMaxWidthRatio, 0.05, 1);
794
+ const maxH = chartHeight * clamp(watermark.imageMaxHeightRatio, 0.05, 1);
795
+ const naturalW = Math.max(1, watermarkImage.naturalWidth || watermarkImage.width);
796
+ const naturalH = Math.max(1, watermarkImage.naturalHeight || watermarkImage.height);
797
+ const containScale = Math.min(maxW / naturalW, maxH / naturalH);
798
+ const scale = Math.max(0.05, containScale * Math.max(0.05, watermark.imageScale));
799
+ const drawW = naturalW * scale;
800
+ const drawH = naturalH * scale;
801
+ const drawX = chartLeft + (chartWidth - drawW) / 2;
802
+ const drawY = chartTop + (chartHeight - drawH) / 2;
803
+ ctx.save();
804
+ ctx.globalAlpha = clamp(watermark.opacity, 0, 1);
805
+ ctx.drawImage(watermarkImage, drawX, drawY, drawW, drawH);
806
+ if (watermark.imageTintColor.trim().length > 0) {
807
+ ctx.globalCompositeOperation = "source-atop";
808
+ ctx.globalAlpha = clamp(watermark.opacity, 0, 1) * clamp(watermark.imageTintOpacity, 0, 1);
809
+ ctx.fillStyle = watermark.imageTintColor;
810
+ ctx.fillRect(drawX, drawY, drawW, drawH);
811
+ }
812
+ ctx.restore();
813
+ }
814
+ }
758
815
  if (watermark.visible && watermark.text.trim().length > 0) {
759
816
  ctx.save();
760
817
  ctx.globalAlpha = clamp(watermark.opacity, 0, 1);
package/dist/index.d.cts CHANGED
@@ -56,6 +56,12 @@ interface WatermarkOptions {
56
56
  fontSize?: number;
57
57
  fontWeight?: number | string;
58
58
  thickness?: number;
59
+ imageSrc?: string;
60
+ imageScale?: number;
61
+ imageMaxWidthRatio?: number;
62
+ imageMaxHeightRatio?: number;
63
+ imageTintColor?: string;
64
+ imageTintOpacity?: number;
59
65
  }
60
66
  interface PriceLineOptions {
61
67
  id?: string;
package/dist/index.d.ts CHANGED
@@ -56,6 +56,12 @@ interface WatermarkOptions {
56
56
  fontSize?: number;
57
57
  fontWeight?: number | string;
58
58
  thickness?: number;
59
+ imageSrc?: string;
60
+ imageScale?: number;
61
+ imageMaxWidthRatio?: number;
62
+ imageMaxHeightRatio?: number;
63
+ imageTintColor?: string;
64
+ imageTintOpacity?: number;
59
65
  }
60
66
  interface PriceLineOptions {
61
67
  id?: string;
package/dist/index.js CHANGED
@@ -27,7 +27,13 @@ var DEFAULT_WATERMARK_OPTIONS = {
27
27
  opacity: 0.14,
28
28
  fontSize: 92,
29
29
  fontWeight: 700,
30
- thickness: 0
30
+ thickness: 0,
31
+ imageSrc: "",
32
+ imageScale: 1,
33
+ imageMaxWidthRatio: 0.42,
34
+ imageMaxHeightRatio: 0.3,
35
+ imageTintColor: "",
36
+ imageTintOpacity: 1
31
37
  };
32
38
  var DEFAULT_PRICE_LINE_OPTIONS = {
33
39
  visible: true,
@@ -158,6 +164,9 @@ function createChart(element, options = {}) {
158
164
  let yMaxOverride = null;
159
165
  let autoYMin = null;
160
166
  let autoYMax = null;
167
+ let watermarkImageSrc = null;
168
+ let watermarkImage = null;
169
+ let watermarkImageReady = false;
161
170
  let drawState = null;
162
171
  let orderDragState = null;
163
172
  let actionDragState = null;
@@ -183,6 +192,29 @@ function createChart(element, options = {}) {
183
192
  }
184
193
  return Math.max(1, window.devicePixelRatio || 1);
185
194
  };
195
+ const ensureWatermarkImage = (src) => {
196
+ if (src === watermarkImageSrc) {
197
+ return;
198
+ }
199
+ watermarkImageSrc = src;
200
+ watermarkImageReady = false;
201
+ watermarkImage = null;
202
+ if (!src) {
203
+ return;
204
+ }
205
+ const image = new Image();
206
+ image.crossOrigin = "anonymous";
207
+ image.onload = () => {
208
+ watermarkImage = image;
209
+ watermarkImageReady = true;
210
+ draw();
211
+ };
212
+ image.onerror = () => {
213
+ watermarkImage = null;
214
+ watermarkImageReady = false;
215
+ };
216
+ image.src = src;
217
+ };
186
218
  const crisp = (value) => Math.round(value) + 0.5;
187
219
  const clamp = (value, min, max) => {
188
220
  return Math.min(max, Math.max(min, value));
@@ -731,6 +763,31 @@ function createChart(element, options = {}) {
731
763
  const yFromPrice = (price) => {
732
764
  return chartBottom - (price - yMin) / yRange * chartHeight;
733
765
  };
766
+ if (watermark.visible && watermark.imageSrc.trim().length > 0) {
767
+ ensureWatermarkImage(watermark.imageSrc.trim());
768
+ if (watermarkImageReady && watermarkImage) {
769
+ const maxW = chartWidth * clamp(watermark.imageMaxWidthRatio, 0.05, 1);
770
+ const maxH = chartHeight * clamp(watermark.imageMaxHeightRatio, 0.05, 1);
771
+ const naturalW = Math.max(1, watermarkImage.naturalWidth || watermarkImage.width);
772
+ const naturalH = Math.max(1, watermarkImage.naturalHeight || watermarkImage.height);
773
+ const containScale = Math.min(maxW / naturalW, maxH / naturalH);
774
+ const scale = Math.max(0.05, containScale * Math.max(0.05, watermark.imageScale));
775
+ const drawW = naturalW * scale;
776
+ const drawH = naturalH * scale;
777
+ const drawX = chartLeft + (chartWidth - drawW) / 2;
778
+ const drawY = chartTop + (chartHeight - drawH) / 2;
779
+ ctx.save();
780
+ ctx.globalAlpha = clamp(watermark.opacity, 0, 1);
781
+ ctx.drawImage(watermarkImage, drawX, drawY, drawW, drawH);
782
+ if (watermark.imageTintColor.trim().length > 0) {
783
+ ctx.globalCompositeOperation = "source-atop";
784
+ ctx.globalAlpha = clamp(watermark.opacity, 0, 1) * clamp(watermark.imageTintOpacity, 0, 1);
785
+ ctx.fillStyle = watermark.imageTintColor;
786
+ ctx.fillRect(drawX, drawY, drawW, drawH);
787
+ }
788
+ ctx.restore();
789
+ }
790
+ }
734
791
  if (watermark.visible && watermark.text.trim().length > 0) {
735
792
  ctx.save();
736
793
  ctx.globalAlpha = clamp(watermark.opacity, 0, 1);
package/docs/API.md CHANGED
@@ -83,11 +83,17 @@ Top-level options:
83
83
 
84
84
  - `visible` (default `false`)
85
85
  - `text` (default `""`)
86
- - `color` (default `#94a3b8`)
87
- - `opacity` (default `0.2`)
86
+ - `color` (default `#81858d`)
87
+ - `opacity` (default `0.14`)
88
88
  - `fontSize` (default `92`)
89
89
  - `fontWeight` (default `700`)
90
90
  - `thickness` (stroke width, default `0`)
91
+ - `imageSrc` (default `""`, URL/path to watermark logo)
92
+ - `imageScale` (default `1`)
93
+ - `imageMaxWidthRatio` (default `0.42`)
94
+ - `imageMaxHeightRatio` (default `0.3`)
95
+ - `imageTintColor` (default `""`, set `"#ffffff"` for white logo tint)
96
+ - `imageTintOpacity` (default `1`)
91
97
 
92
98
  ### `TickerLineOptions`
93
99
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hyperprop-charting-library",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Lightweight TypeScript charting core",
5
5
  "type": "module",
6
6
  "main": "./dist/hyperprop-charting-library.cjs",