cc-gram 1.2.2 → 1.2.4
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/README.md +1 -1
- package/dist/core.d.ts +3 -3
- package/dist/filters.d.ts +1 -1
- package/dist/index.d.cts +10 -0
- package/dist/index.esm.js +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/utils.d.ts +2 -3
- package/package.json +36 -25
- package/src/core.ts +5 -5
- package/src/utils.ts +4 -4
package/README.md
CHANGED
package/dist/core.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { FilterName, FilterSetting } from './filters';
|
|
2
|
-
import { Options, ParseOptions } from './types';
|
|
1
|
+
import { type FilterName, type FilterSetting } from './filters';
|
|
2
|
+
import type { Options, ParseOptions } from './types';
|
|
3
3
|
export declare const DEFAULT_DATA_ATTRIBUTE = "filter";
|
|
4
4
|
/** 🖼 A CSS & Canvas Instagram filters based on CSSgram */
|
|
5
5
|
export declare class CCgram {
|
|
@@ -58,5 +58,5 @@ export declare class CCgram {
|
|
|
58
58
|
export declare const CCGram: typeof CCgram;
|
|
59
59
|
/** alias for `CCgram` */
|
|
60
60
|
export declare const Filter: typeof CCgram;
|
|
61
|
-
export
|
|
61
|
+
export type FilterInstance = InstanceType<typeof CCgram>;
|
|
62
62
|
export declare function createFilter(options: Options): FilterInstance;
|
package/dist/filters.d.ts
CHANGED
package/dist/index.d.cts
ADDED
package/dist/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
const t=new Map([["aden",{"hue-rotate":-20,contrast:.9,brightness:1.2,saturate:.85}],["inkwell",{sepia:.3,contrast:1.1,brightness:1.1,grayscale:1}],["reyes",{sepia:.22,contrast:.85,brightness:1.1,saturate:.75}],["gingham",{"hue-rotate":-10,brightness:1.05}],["toaster",{contrast:1.5,brightness:.9}],["walden",{"hue-rotate":-10,brightness:1.1,sepia:.3,saturate:1.6}],["hudson",{brightness:1.2,contrast:.9,saturate:1.1}],["earlybird",{contrast:.9,sepia:.2}],["mayfair",{contrast:1.1,saturate:1.1}],["lofi",{contrast:1.5,saturate:1.1}],["1977",{contrast:1.1,brightness:1.1,saturate:1.3}],["brooklyn",{contrast:.9,brightness:1.1}],["xpro2",{sepia:.3}],["nashville",{contrast:1.2,brightness:1.05,saturate:1.2,sepia:.2}],["lark",{contrast:.9}],["moon",{brightness:1.1,contrast:1.1,grayscale:1}],["clarendon",{contrast:1.2,saturate:1.35}],["willow",{contrast:.95,brightness:.9,grayscale:.5}],["rise",{contrast:.9,brightness:1.05,sepia:.2,saturate:.9}],["slumber",{brightness:1.05,saturate:.66}],["brannan",{contrast:1.4,sepia:.5}],["valencia",{contrast:1.08,brightness:1.08,sepia:.08}],["maven",{contrast:.95,brightness:1.95,saturate:1.5,sepia:.25}],["stinson",{contrast:.75,brightness:1.15,saturate:.85}],["amaro",{"hue-rotate":-10,contrast:.9,brightness:1.1,saturate:1.5}]]),e="undefined"!=typeof OffscreenCanvas;function r(t,e="internal error."){if(!t)throw Error(`[CCgram] ${e}`)}function a(t){r(t&&"IMG"===t.tagName,"The first argument is required and must be an <img> element."),r(t.src,"The <img> element src attribute is empty.")}function s({data:t}){const{canvas:e,image:r,filterStyle:a,options:s}=t,n=e.getContext("2d",{alpha:!1});if(!n)throw new Error("The 2d context canvas is not supported.");return n.filter=a,n.drawImage(r,0,0),e.convertToBlob(s)}const n="filter";class i{static DEFAULT_DATA_ATTRIBUTE=n;static DEFAULT_FILTERS=t;_filters=t;_dataAttribute;_dataAttributeKey;constructor({dataAttribute:t=n,init:e=!0}={}){if(this._dataAttribute=t,this._dataAttributeKey=t.replace(/-./g,t=>t[1]?t[1].toUpperCase():""),!e)return;if("complete"===document.readyState)return void this.applyFilter();const r=()=>{this.applyFilter(),document.removeEventListener("DOMContentLoaded",r)};document.addEventListener("DOMContentLoaded",r)}get filterNames(){return[...this._filters.keys()]}setFilter(t,e){this._filters.set(t,e)}removeFilter(t){return this._filters.delete(t)}getFilterSetting(t=""){return this._filters.get(t)}getFilterStyle(t=""){return function(t){return t?Object.keys(t).map(e=>`${e}(${t[e]}${"hue-rotate"===e?"deg":"blur"===e?"px":""})`).join(" "):"none"}(this._filters.get(t))}applyFilter(t=`img[data-${this._dataAttribute}]`){document.querySelectorAll(t).forEach(t=>{const{dataset:e}=t;t.style.setProperty("filter",this.getFilterStyle(e[this._dataAttributeKey]))})}async getDataURL(t,e={}){a(t);const r=await this.getBlob(t,e);if(!r)return null;const s=new FileReader;return new Promise(t=>{s.addEventListener("load",()=>{t(s.result)}),s.readAsDataURL(r)})}async getBlob(t,r={}){a(t);const{naturalWidth:n,naturalHeight:i}=t,o=r.filter??t.dataset[this._dataAttributeKey],c=this.getFilterStyle(o);if(e){const e=new OffscreenCanvas(n,i),a=await createImageBitmap(t);return new Promise(t=>{const n=function(t){const e=`\n const work = ${t.toString()};\n\n addEventListener('message', async (...params) => {\n const res = await work(...params);\n postMessage(res);\n });\n `,r=URL.createObjectURL(new Blob([e],{type:"text/javascript"})),a=new Worker(r),{terminate:s}=a;return a.terminate=()=>{URL.revokeObjectURL(r),s.call(a)},a}(s);n.addEventListener("message",({data:e})=>{t(e),n.terminate()}),n.postMessage({canvas:e,image:a,filterStyle:c,options:r},[e,a])})}const l=document.createElement("canvas");l.width=n,l.height=i;const u=l.getContext("2d",{alpha:!1});if(!u)throw new Error("The 2d context canvas is not supported.");u.filter=c,u.drawImage(t,0,0);const{type:d,quality:g}=r;return new Promise(t=>l.toBlob(e=>t(e),d,g))}}const o=i,c=i;function l(t){return new c(t)}export{o as CCGram,i as CCgram,n as DEFAULT_DATA_ATTRIBUTE,t as DEFAULT_FILTERS,c as Filter,l as createFilter,c as default};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).CCGram={})}(this,
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).CCGram={})}(this,function(t){"use strict";const e=new Map([["aden",{"hue-rotate":-20,contrast:.9,brightness:1.2,saturate:.85}],["inkwell",{sepia:.3,contrast:1.1,brightness:1.1,grayscale:1}],["reyes",{sepia:.22,contrast:.85,brightness:1.1,saturate:.75}],["gingham",{"hue-rotate":-10,brightness:1.05}],["toaster",{contrast:1.5,brightness:.9}],["walden",{"hue-rotate":-10,brightness:1.1,sepia:.3,saturate:1.6}],["hudson",{brightness:1.2,contrast:.9,saturate:1.1}],["earlybird",{contrast:.9,sepia:.2}],["mayfair",{contrast:1.1,saturate:1.1}],["lofi",{contrast:1.5,saturate:1.1}],["1977",{contrast:1.1,brightness:1.1,saturate:1.3}],["brooklyn",{contrast:.9,brightness:1.1}],["xpro2",{sepia:.3}],["nashville",{contrast:1.2,brightness:1.05,saturate:1.2,sepia:.2}],["lark",{contrast:.9}],["moon",{brightness:1.1,contrast:1.1,grayscale:1}],["clarendon",{contrast:1.2,saturate:1.35}],["willow",{contrast:.95,brightness:.9,grayscale:.5}],["rise",{contrast:.9,brightness:1.05,sepia:.2,saturate:.9}],["slumber",{brightness:1.05,saturate:.66}],["brannan",{contrast:1.4,sepia:.5}],["valencia",{contrast:1.08,brightness:1.08,sepia:.08}],["maven",{contrast:.95,brightness:1.95,saturate:1.5,sepia:.25}],["stinson",{contrast:.75,brightness:1.15,saturate:.85}],["amaro",{"hue-rotate":-10,contrast:.9,brightness:1.1,saturate:1.5}]]),r="undefined"!=typeof OffscreenCanvas;function a(t,e="internal error."){if(!t)throw Error(`[CCgram] ${e}`)}function s(t){a(t&&"IMG"===t.tagName,"The first argument is required and must be an <img> element."),a(t.src,"The <img> element src attribute is empty.")}function n({data:t}){const{canvas:e,image:r,filterStyle:a,options:s}=t,n=e.getContext("2d",{alpha:!1});if(!n)throw new Error("The 2d context canvas is not supported.");return n.filter=a,n.drawImage(r,0,0),e.convertToBlob(s)}const i="filter";class o{static DEFAULT_DATA_ATTRIBUTE=i;static DEFAULT_FILTERS=e;_filters=e;_dataAttribute;_dataAttributeKey;constructor({dataAttribute:t=i,init:e=!0}={}){if(this._dataAttribute=t,this._dataAttributeKey=t.replace(/-./g,t=>t[1]?t[1].toUpperCase():""),!e)return;if("complete"===document.readyState)return void this.applyFilter();const r=()=>{this.applyFilter(),document.removeEventListener("DOMContentLoaded",r)};document.addEventListener("DOMContentLoaded",r)}get filterNames(){return[...this._filters.keys()]}setFilter(t,e){this._filters.set(t,e)}removeFilter(t){return this._filters.delete(t)}getFilterSetting(t=""){return this._filters.get(t)}getFilterStyle(t=""){return function(t){return t?Object.keys(t).map(e=>`${e}(${t[e]}${"hue-rotate"===e?"deg":"blur"===e?"px":""})`).join(" "):"none"}(this._filters.get(t))}applyFilter(t=`img[data-${this._dataAttribute}]`){document.querySelectorAll(t).forEach(t=>{const{dataset:e}=t;t.style.setProperty("filter",this.getFilterStyle(e[this._dataAttributeKey]))})}async getDataURL(t,e={}){s(t);const r=await this.getBlob(t,e);if(!r)return null;const a=new FileReader;return new Promise(t=>{a.addEventListener("load",()=>{t(a.result)}),a.readAsDataURL(r)})}async getBlob(t,e={}){s(t);const{naturalWidth:a,naturalHeight:i}=t,o=e.filter??t.dataset[this._dataAttributeKey],c=this.getFilterStyle(o);if(r){const r=new OffscreenCanvas(a,i),s=await createImageBitmap(t);return new Promise(t=>{const a=function(t){const e=`\n const work = ${t.toString()};\n\n addEventListener('message', async (...params) => {\n const res = await work(...params);\n postMessage(res);\n });\n `,r=URL.createObjectURL(new Blob([e],{type:"text/javascript"})),a=new Worker(r),{terminate:s}=a;return a.terminate=()=>{URL.revokeObjectURL(r),s.call(a)},a}(n);a.addEventListener("message",({data:e})=>{t(e),a.terminate()}),a.postMessage({canvas:r,image:s,filterStyle:c,options:e},[r,s])})}const l=document.createElement("canvas");l.width=a,l.height=i;const u=l.getContext("2d",{alpha:!1});if(!u)throw new Error("The 2d context canvas is not supported.");u.filter=c,u.drawImage(t,0,0);const{type:d,quality:g}=e;return new Promise(t=>l.toBlob(e=>t(e),d,g))}}const c=o,l=o;t.CCGram=c,t.CCgram=o,t.DEFAULT_DATA_ATTRIBUTE=i,t.DEFAULT_FILTERS=e,t.Filter=l,t.createFilter=function(t){return new l(t)},t.default=l,Object.defineProperty(t,"__esModule",{value:!0})});
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import { ParseOptions } from './types';
|
|
1
|
+
import type { FilterSetting } from './filters';
|
|
2
|
+
import type { ParseOptions } from './types';
|
|
4
3
|
export declare const hasOffscreenCanvas: boolean;
|
|
5
4
|
export declare function camelize(string: string): string;
|
|
6
5
|
export declare function assert<TCond = unknown>(condition: TCond, message?: string): asserts condition;
|
package/package.json
CHANGED
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cc-gram",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"description": "🖼 A CSS & Canvas Instagram filters based on CSSgram",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": "^
|
|
7
|
+
"node": "^20.19.0 || ^22.12.0 || >=24.0.0"
|
|
8
8
|
},
|
|
9
9
|
"packageManager": "pnpm@9.1.0",
|
|
10
10
|
"main": "dist/index.umd.js",
|
|
11
11
|
"module": "dist/index.esm.js",
|
|
12
12
|
"types": "dist/index.d.ts",
|
|
13
13
|
"exports": {
|
|
14
|
-
"import":
|
|
15
|
-
|
|
14
|
+
"import": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.esm.js"
|
|
17
|
+
},
|
|
18
|
+
"require": {
|
|
19
|
+
"types": "./dist/index.d.cts",
|
|
20
|
+
"default": "./dist/index.umd.js"
|
|
21
|
+
}
|
|
16
22
|
},
|
|
17
23
|
"files": [
|
|
18
24
|
"dist",
|
|
@@ -20,13 +26,15 @@
|
|
|
20
26
|
],
|
|
21
27
|
"scripts": {
|
|
22
28
|
"build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
|
|
23
|
-
"
|
|
29
|
+
"typecheck": "tsc --noEmit",
|
|
24
30
|
"test": "vitest __tests__",
|
|
25
31
|
"test:coverage": "vitest run __tests__ --coverage",
|
|
26
|
-
"lint": "eslint --fix
|
|
32
|
+
"lint": "eslint --fix src",
|
|
33
|
+
"lint:pkg": "npx publint",
|
|
27
34
|
"prepublishOnly": "pnpm build",
|
|
28
35
|
"release": "sh scripts/release.sh",
|
|
29
|
-
"demo:dev": "pnpm -C demo dev"
|
|
36
|
+
"demo:dev": "pnpm -C demo dev",
|
|
37
|
+
"demo:build": "pnpm -C demo build"
|
|
30
38
|
},
|
|
31
39
|
"keywords": [
|
|
32
40
|
"image",
|
|
@@ -42,7 +50,7 @@
|
|
|
42
50
|
"license": "MIT",
|
|
43
51
|
"repository": {
|
|
44
52
|
"type": "git",
|
|
45
|
-
"url": "https://github.com/EastSun5566/cc-gram"
|
|
53
|
+
"url": "git+https://github.com/EastSun5566/cc-gram.git"
|
|
46
54
|
},
|
|
47
55
|
"homepage": "https://eastsun5566.github.io/cc-gram/",
|
|
48
56
|
"bugs": {
|
|
@@ -54,24 +62,27 @@
|
|
|
54
62
|
}
|
|
55
63
|
},
|
|
56
64
|
"devDependencies": {
|
|
57
|
-
"@rollup/plugin-node-resolve": "^
|
|
58
|
-
"@rollup/plugin-terser": "^0.
|
|
59
|
-
"@rollup/plugin-typescript": "^
|
|
60
|
-
"@
|
|
61
|
-
"@
|
|
62
|
-
"@
|
|
63
|
-
"@vitest/coverage-
|
|
64
|
-
"
|
|
65
|
-
"eslint": "^
|
|
66
|
-
"eslint-config-airbnb-
|
|
67
|
-
"eslint-
|
|
65
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
66
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
67
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
68
|
+
"@stylistic/eslint-plugin": "^3.0.1",
|
|
69
|
+
"@types/node": "^24.10.4",
|
|
70
|
+
"@types/offscreencanvas": "^2019.7.3",
|
|
71
|
+
"@vitest/coverage-v8": "^4.0.16",
|
|
72
|
+
"@vitest/ui": "^4.0.16",
|
|
73
|
+
"eslint": "^9.39.2",
|
|
74
|
+
"eslint-config-airbnb-extended": "^2.3.3",
|
|
75
|
+
"eslint-import-resolver-typescript": "^4.0.0",
|
|
76
|
+
"eslint-plugin-import-x": "^4.6.1",
|
|
77
|
+
"globals": "^15.14.0",
|
|
68
78
|
"husky": "^3.1.0",
|
|
69
|
-
"jsdom": "^
|
|
70
|
-
"rimraf": "^
|
|
71
|
-
"rollup": "^
|
|
79
|
+
"jsdom": "^27.4.0",
|
|
80
|
+
"rimraf": "^6.1.2",
|
|
81
|
+
"rollup": "^4.54.0",
|
|
72
82
|
"rollup-plugin-filesize": "^9.1.2",
|
|
73
|
-
"tslib": "^2.
|
|
74
|
-
"typescript": "^
|
|
75
|
-
"
|
|
83
|
+
"tslib": "^2.8.1",
|
|
84
|
+
"typescript": "^5.9.3",
|
|
85
|
+
"typescript-eslint": "^8.50.1",
|
|
86
|
+
"vitest": "^4.0.16"
|
|
76
87
|
}
|
|
77
88
|
}
|
package/src/core.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
DEFAULT_FILTERS,
|
|
3
|
-
FilterName,
|
|
4
|
-
FilterSetting,
|
|
3
|
+
type FilterName,
|
|
4
|
+
type FilterSetting,
|
|
5
5
|
} from './filters';
|
|
6
6
|
import {
|
|
7
7
|
parseSettingToStyle,
|
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
camelize,
|
|
13
13
|
} from './utils';
|
|
14
14
|
|
|
15
|
-
import { Options, ParseOptions } from './types';
|
|
15
|
+
import type { Options, ParseOptions } from './types';
|
|
16
16
|
|
|
17
17
|
export const DEFAULT_DATA_ATTRIBUTE = 'filter';
|
|
18
18
|
|
|
@@ -97,8 +97,8 @@ export class CCgram {
|
|
|
97
97
|
* Apply CSS filter to all targets
|
|
98
98
|
* @param [selectors='img[data-${this._dataAttribute}]'] - selectors
|
|
99
99
|
*/
|
|
100
|
-
|
|
101
|
-
applyFilter(selectors
|
|
100
|
+
|
|
101
|
+
applyFilter(selectors = `img[data-${this._dataAttribute}]`): void {
|
|
102
102
|
document
|
|
103
103
|
.querySelectorAll<HTMLImageElement>(selectors)
|
|
104
104
|
.forEach((target): void => {
|
package/src/utils.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { FilterSetting } from './filters';
|
|
2
|
-
import { ParseOptions } from './types';
|
|
1
|
+
import type { FilterSetting } from './filters';
|
|
2
|
+
import type { ParseOptions } from './types';
|
|
3
3
|
|
|
4
4
|
export const hasOffscreenCanvas = typeof OffscreenCanvas !== 'undefined';
|
|
5
5
|
|
|
@@ -18,7 +18,7 @@ export function assertIsImage(image: HTMLImageElement): asserts image is HTMLIma
|
|
|
18
18
|
|
|
19
19
|
export function createWorker<
|
|
20
20
|
TData = unknown,
|
|
21
|
-
TMessage = unknown
|
|
21
|
+
TMessage = unknown,
|
|
22
22
|
>(fn: (messageEvent: MessageEvent<TData>) => TMessage): Worker {
|
|
23
23
|
const code = `
|
|
24
24
|
const work = ${fn.toString()};
|
|
@@ -61,7 +61,7 @@ export function parseSettingToStyle(setting?: FilterSetting): string {
|
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
interface CreateBlobOptions<
|
|
64
|
-
TCanvas extends HTMLCanvasElement | OffscreenCanvas = HTMLCanvasElement
|
|
64
|
+
TCanvas extends HTMLCanvasElement | OffscreenCanvas = HTMLCanvasElement,
|
|
65
65
|
> {
|
|
66
66
|
canvas: TCanvas;
|
|
67
67
|
image: CanvasImageSource;
|