Socket
Socket
Sign inDemoInstall

canvas-effects

Package Overview
Dependencies
3
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.3 to 0.2.4

dist/helpers.d.ts

3

dist/CanvasEffect/index.d.ts
import { Config } from '../types';
export default abstract class CanvasEffect<TConfig extends Config> {
protected readonly config: TConfig;
protected config: TConfig;
protected canvas: HTMLCanvasElement;

@@ -12,2 +12,3 @@ protected ctx: CanvasRenderingContext2D | null;

protected init(): void;
updateConfig(config: TConfig): void;
protected abstract render(): void;

@@ -14,0 +15,0 @@ protected clear(): void;

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var requestAnimationFrame_1 = require("./requestAnimationFrame");
var validate = __importStar(require("./validate"));
var CanvasEffect = /** @class */ (function () {

@@ -18,2 +42,10 @@ function CanvasEffect(item, config) {

};
CanvasEffect.prototype.updateConfig = function (config) {
if (config.width && (config.width === Infinity || validate.number(config.width))) {
this.config.width = config.width;
}
if (config.height && (config.height === Infinity || validate.number(config.height))) {
this.config.height = config.height;
}
};
CanvasEffect.prototype.clear = function () {

@@ -20,0 +52,0 @@ if (this.ctx != null) {

@@ -12,6 +12,7 @@ import CanvasEffect from '../CanvasEffect';

protected init(): void;
updateConfig(config: DelaunayConfig): void;
protected render(): void;
private generate;
private triangulate;
private getLightSource;
private getCenterLight;
private getApexHeight;

@@ -18,0 +19,0 @@ private getComplexity;

@@ -54,3 +54,3 @@ "use strict";

var _this = _super.call(this, item, config) || this;
_this.seed = 16000;
_this.seed = 5000;
if (config.seed && validate.number(config.seed)) {

@@ -61,6 +61,3 @@ _this.seed = config.seed;

_this.complexity = _this.getComplexity(_this.seed);
_this.light = _this.getLightSource(_this.apex);
if (!validate.boolean(config.mouse) || config.mouse === true) {
_this.canvas.addEventListener('mousemove', _this.onMouseMove.bind(_this), false);
}
_this.light = _this.getCenterLight(_this.apex);
_this.simplex = (0, open_simplex_noise_1.makeNoise2D)(Date.now());

@@ -73,4 +70,44 @@ _this.triangles = [];

this.generate();
this.canvas.addEventListener('mousemove', this.onMouseMove.bind(this), false);
_super.prototype.init.call(this);
};
Delaunay.prototype.updateConfig = function (config) {
_super.prototype.updateConfig.call(this, config);
if (config.seed && validate.number(config.seed) && (config.seed != this.config.seed)) {
this.config.seed = config.seed;
this.complexity = this.getComplexity(config.seed);
this.triangles = [];
this.generate();
}
if (config.color && validate.color(config.color)) {
this.config.color = config.color;
}
if (validate.boolean(config.mouse)) {
this.config.mouse = config.mouse;
}
if (config.shade && validate.number(config.shade)) {
this.config.shade = config.shade;
}
if (config.stroke && config.stroke.color && validate.color(config.stroke.color)) {
if (this.config.stroke) {
this.config.stroke.color = config.stroke.color;
}
else {
this.config.stroke = { color: config.stroke.color };
}
}
if (config.stroke && config.stroke.width && validate.number(config.stroke.width)) {
if (this.config.stroke) {
this.config.stroke.width = config.stroke.width;
}
else {
this.config.stroke = { width: config.stroke.width };
}
}
if (config.color || config.stroke) {
for (var t = 0; t < this.triangles.length; t++) {
this.triangles[t].init(config);
}
}
};
Delaunay.prototype.render = function () {

@@ -118,3 +155,3 @@ _super.prototype.clear.call(this);

};
Delaunay.prototype.getLightSource = function (height) {
Delaunay.prototype.getCenterLight = function (height) {
return [

@@ -130,3 +167,3 @@ this.canvas.width / 2,

Delaunay.prototype.getComplexity = function (seed) {
return Math.floor(this.canvas.width * this.canvas.height / seed);
return Math.floor((this.canvas.width * this.canvas.height * 5) / (seed));
};

@@ -147,4 +184,6 @@ Delaunay.prototype.getRandomArbitrary = function (max, min) {

Delaunay.prototype.onMouseMove = function (e) {
var pos = this.getMousePosition(e);
this.light = [pos[0], pos[1], this.light[2]];
if (this.config.mouse) {
var pos = this.getMousePosition(e);
this.light = [pos[0], pos[1], this.light[2]];
}
};

@@ -151,0 +190,0 @@ return Delaunay;

@@ -10,3 +10,3 @@ import { DelaunayConfig, Point3D, ColorRGBA } from '../types';

hue: ColorRGBA;
max: number;
shade: number;
stroke: {

@@ -23,3 +23,3 @@ color?: ColorRGBA;

normalize(v: Point3D): Point3D;
shade(color: number[], i: number): ColorRGBA;
getShadeColor(color: number[], value: number): ColorRGBA;
getIntensity(power: number, max: number): number;

@@ -26,0 +26,0 @@ dotProduct(v1: number[], v2: number[]): number;

@@ -27,2 +27,3 @@ "use strict";

var validate = __importStar(require("../CanvasEffect/validate"));
var helpers = __importStar(require("../helpers"));
/*

@@ -42,3 +43,3 @@ * SOURCES

this.hue = this.color;
this.max = 0.5;
this.shade = 0.5;
this.stroke = {};

@@ -50,5 +51,5 @@ }

}
if (config.max && validate.number(config.max)) {
if (config.max >= 0 && config.max <= 1) {
this.max = config.max;
if (config.shade && validate.number(config.shade)) {
if (config.shade >= 0 && config.shade <= 1) {
this.shade = config.shade;
}

@@ -75,8 +76,8 @@ }

var power = 1 - (dp + 1) / 2;
this.hue = this.shade(this.color, this.getIntensity(power, this.max));
this.hue = this.getShadeColor(this.color, this.getIntensity(power, this.shade));
};
Triangle.prototype.render = function () {
this.ctx.fillStyle = "rgba(".concat(this.hue[0], ",").concat(this.hue[1], ",").concat(this.hue[2], ",").concat(this.hue[3], ")");
this.ctx.fillStyle = helpers.colorStyle(this.hue);
if (this.stroke.color) {
this.ctx.strokeStyle = "rgba(".concat(this.stroke.color[0], ",").concat(this.stroke.color[1], ",").concat(this.stroke.color[2], ",").concat(this.stroke.color[3], ")");
this.ctx.strokeStyle = helpers.colorStyle(this.stroke.color);
if (this.stroke.width) {

@@ -87,3 +88,3 @@ this.ctx.lineWidth = this.stroke.width;

else {
this.ctx.strokeStyle = "rgba(".concat(this.hue[0], ",").concat(this.hue[1], ",").concat(this.hue[2], ",").concat(this.hue[3], ")");
this.ctx.strokeStyle = helpers.colorStyle(this.hue);
}

@@ -112,10 +113,14 @@ this.ctx.beginPath();

Triangle.prototype.normalize = function (v) {
var m = Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
return [v[0] / m, v[1] / m, v[2] / m];
var m = Math.sqrt((v[0] * v[0]) + (v[1] * v[1]) + (v[2] * v[2]));
return [
v[0] / m,
v[1] / m,
v[2] / m
];
};
Triangle.prototype.shade = function (color, i) {
Triangle.prototype.getShadeColor = function (color, value) {
return [
Math.floor(color[0] * i),
Math.floor(color[1] * i),
Math.floor(color[2] * i),
Math.floor(color[0] * value),
Math.floor(color[1] * value),
Math.floor(color[2] * value),
color[3]

@@ -125,6 +130,6 @@ ];

Triangle.prototype.getIntensity = function (power, max) {
return 1 - max + max * power;
return 1 - max + (max * power);
};
Triangle.prototype.dotProduct = function (v1, v2) {
return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
return (v1[0] * v2[0]) + (v1[1] * v2[1]) + (v1[2] * v2[2]);
};

@@ -131,0 +136,0 @@ Triangle.prototype.getCenteroid = function () {

@@ -9,2 +9,3 @@ import CanvasEffect from '../CanvasEffect';

constructor(item: HTMLCanvasElement, config: GalileoConfig);
updateConfig(config: GalileoConfig): void;
protected render(): void;

@@ -11,0 +12,0 @@ private generate;

@@ -53,3 +53,3 @@ "use strict";

var _this = _super.call(this, item, config) || this;
_this.seed = 8000;
_this.seed = 5000;
_this.complexity = _this.getComplexity(_this.seed);

@@ -65,2 +65,22 @@ if (config.seed && validate.number(config.seed)) {

}
Galileo.prototype.updateConfig = function (config) {
_super.prototype.updateConfig.call(this, config);
if (config.seed && validate.number(config.seed) && (config.seed != this.config.seed)) {
this.config.seed = config.seed;
this.complexity = this.getComplexity(config.seed);
this.lines = [];
this.points = [];
this.generate();
}
if (config.point) {
for (var p = 0; p < this.points.length; p++) {
this.points[p].init(config.point);
}
}
if (config.line) {
for (var l = 0; l < this.lines.length; l++) {
this.lines[l].init(config.line);
}
}
};
Galileo.prototype.render = function () {

@@ -67,0 +87,0 @@ _super.prototype.clear.call(this);

@@ -27,2 +27,3 @@ "use strict";

var validate = __importStar(require("../CanvasEffect/validate"));
var VELOCITY_MOD = 0.01;
var Point = /** @class */ (function () {

@@ -35,4 +36,4 @@ function Point(ctx, pos) {

this.color = [0, 0, 0, 1];
this.radius = this.getRandomArbitrary(4, 2);
this.velocity = this.getRandomArbitrary(0.2, 0.1);
this.radius = this.getRandomArbitrary(2);
this.velocity = this.getRandomArbitrary(10);
this.theta = this.getRandomTheta();

@@ -45,11 +46,7 @@ }

}
if (config.radius && validate.array(config.radius, 2)) {
if (config.radius[0] > config.radius[1]) {
this.radius = this.getRandomArbitrary(config.radius[0], config.radius[1]);
}
if (config.radius && validate.number(config.radius)) {
this.radius = this.getRandomArbitrary(config.radius);
}
if (config.velocity && validate.array(config.velocity, 2)) {
if (config.velocity[0] > config.velocity[1]) {
this.velocity = this.getRandomArbitrary(config.velocity[0], config.velocity[1]);
}
if (config.velocity && validate.number(config.velocity)) {
this.velocity = this.getRandomArbitrary(config.velocity);
}

@@ -63,6 +60,6 @@ }

if (this.pos[1] <= 0 + this.radius || this.pos[1] >= this.ch - this.radius) {
this.theta = 2 * Math.PI - this.theta;
this.theta = (2 * Math.PI) - this.theta;
}
this.pos[0] += Math.cos(this.theta) * this.velocity;
this.pos[1] += Math.sin(this.theta) * this.velocity;
this.pos[0] += Math.cos(this.theta) * this.velocity * VELOCITY_MOD;
this.pos[1] += Math.sin(this.theta) * this.velocity * VELOCITY_MOD;
};

@@ -78,3 +75,6 @@ Point.prototype.render = function () {

};
Point.prototype.getRandomArbitrary = function (max, min) {
Point.prototype.getRandomArbitrary = function (n) {
var margin = 0.2;
var max = n + (n * margin);
var min = n - (n * margin);
return Math.random() * (max - min) + min;

@@ -81,0 +81,0 @@ };

@@ -0,7 +1,7 @@

export type Point3D = [number, number, number];
export type ColorRGBA = [number, number, number, number];
export interface Config {
height: number;
width: number;
height: number;
}
export type Point3D = [number, number, number];
export type ColorRGBA = [number, number, number, number];
export interface GalileoConfig extends Config {

@@ -14,4 +14,4 @@ seed?: number;

color?: ColorRGBA;
radius?: [number, number];
velocity?: [number, number];
radius?: number;
velocity?: number;
}

@@ -28,3 +28,3 @@ export interface LineConfig {

mouse?: boolean;
max?: number;
shade?: number;
stroke?: {

@@ -31,0 +31,0 @@ color?: ColorRGBA;

{
"name": "canvas-effects",
"description": "modular canvas visualizations",
"version": "0.2.3",
"version": "0.2.4",
"main": "dist/index.js",

@@ -6,0 +6,0 @@ "types": "dist/index.d.ts",

# canvas-effects
Modular canvas visualizations. Written in Typescript.
A modular HTML Canvas library.
[DEMO](https://micahco.github.io/canvas-effects)
[DEMO (React)](https://micahco.github.io/canvas-effects)
[![npm version](https://badge.fury.io/js/canvas-effects.svg)](https://badge.fury.io/js/canvas-effects)
## Getting Started
install:
## Usage
`yarn add canvas-effects`
usage:
```
import { Stars } from 'canvas-effects'
import { Effect } from 'canvas-effects'
const stars = new Stars({
width: Infinity,
height: Infinity,
point: {
color: [184, 142, 141, 1]
},
line: {
color: [216, 210, 225, 1],
fade: false
}
});
new Effect(HTMLCanvasElement, Config);
const el = document.getElementById('stars');
if (el != null) {
el.appendChild(stars.canvas);
}
```
# Config
## Config

@@ -48,14 +28,10 @@ These are the required properties.

Setting the width or height value to `Infinity` will fit the element to the page.
Setting the width or height value to `Infinity` will fill the respective viewport.
```
const foo = new Effect({
width: 500,
const foo = new Effect(bar, {
width: Infinity,
height: 500
});
const bar = new Effect({
width: Infinity,
height: 400
});
```

@@ -66,3 +42,3 @@

# Stars
### Galileo

@@ -75,3 +51,2 @@ **seed** : number

***point*** : {Object}

@@ -81,19 +56,18 @@

ColorRGBA [r, g, b, a] of point.
RGBA value of point.
Default: [0, 0, 0, 1]
**radius** : [number, number]
**radius** : number
Range [max, min] of point radius.
Range tuple [max, min] of point radius.
Default: [4, 2]
Default: 2
**velocity** : [number, number]
**velocity** : number
Range [max, min] of the velocity at which the vertices travel.
Range tuple [max, min] of the velocity at which the vertices travel.
Default: [0.2, 0.1]`
Default: 10
***line*** : {Object}

@@ -103,3 +77,3 @@

ColorRGBA [r, g, b, a] of line.
RGBA value of line.

@@ -112,5 +86,4 @@ Default: [0, 0, 0, 1]

Default: true`
Default: true
**max** : number

@@ -126,6 +99,6 @@

Default: 1`
Default: 1
# Polygonal
### Delaunay

@@ -148,5 +121,5 @@ **seed** : number

Default: true
Default: false
**max** : number
**shade** : number

@@ -171,3 +144,3 @@ A number from 0 to 1 representing the max shade value. If value is 1, then polygons will be completely black when hidden from the light source. If the value is 0, the light source will not affect the polygons at all.

Default: undefined`
Default: undefined

@@ -177,6 +150,6 @@

Math Formulas: [Daniel Avila](https://github.com/danthecodingman)
Some math: [Daniel Avila](https://github.com/danthecodingman)
## License
[BSD-2-Clause](LICENSE)
[MIT](LICENSE)

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc