Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pit-js

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pit-js - npm Package Compare versions

Comparing version 1.9.1 to 2.0.0

src/Region.js

708

examples/build/PIT.js

@@ -5,2 +5,270 @@ 'use strict';

class Vector2
{
constructor(x = 0, y = 0)
{
this.x = x;
this.y = y;
}
set(x, y)
{
this.x = x;
this.y = y;
return this;
}
clone()
{
return new this.constructor(this.x, this.y);
}
copy(v)
{
this.x = v.x;
this.y = v.y;
return this;
}
add(v)
{
this.x += v.x;
this.y += v.y;
return this;
}
sub(v)
{
this.x -= v.x;
this.y -= v.y;
return this;
}
multiplyScalar(scalar)
{
this.x *= scalar;
this.y *= scalar;
return this;
}
divide(v)
{
this.x /= v.x;
this.y /= v.y;
return this;
}
divideScalar(scalar)
{
return this.multiplyScalar(1 / scalar);
}
dot(v)
{
return this.x * v.x + this.y * v.y;
}
cross(v)
{
return this.x * v.y - this.y * v.x;
}
lengthSq()
{
return this.x * this.x + this.y * this.y;
}
length()
{
return Math.sqrt(this.x * this.x + this.y * this.y);
}
normalize()
{
return this.divideScalar(this.length() || 1);
}
angle()
{
// computes the angle in radians with respect to the positive x-axis
const angle = Math.atan2(-this.y, -this.x) + Math.PI;
return angle;
}
distanceTo(v)
{
return Math.sqrt(this.distanceToSquared(v));
}
distanceToSquared(v)
{
const dx = this.x - v.x; const dy = this.y - v.y;
return dx * dx + dy * dy;
}
lerp(v, alpha)
{
this.x += (v.x - this.x) * alpha;
this.y += (v.y - this.y) * alpha;
return this;
}
}
Vector2.prototype.isVector2 = true;
class LimitedStack
{
constructor(max_size = 1)
{
this.max_size = max_size;
this.array = [];
}
get_first()
{
return this.array[0];
}
length()
{
return this.array.length();
}
set_from_stack(stack)
{
for (let i = 0; i < this.array.length; i++)
{
this.array[i] = stack.array[i];
}
}
push(elem)
{
this.array.unshift(elem);
if (this.array.length > this.max_size)
{
this.array.pop();
}
}
}
class LimitedVector2Stack extends LimitedStack
{
constructor(max_size = 1)
{
super(max_size);
this.average = new Vector2();
}
set_from_stack(vector2_stack)
{
for (let i = 0; i < this.array.length; i++)
{
this.array[i].copy(vector2_stack.array[i]);
}
this.update_average();
}
push(elem)
{
super.push(elem);
this.update_average();
}
update_average()
{
this.average.set(0, 0);
for (let i = 0; i < this.array.length; i++)
{
this.average.add(this.array[i]);
}
this.average.divideScalar(Math.max(1, this.array.length));
}
}
class Pointer
{
constructor(id, x, y, region)
{
this.region = region;
this.id = id;
this.position_array = new LimitedVector2Stack(5);
this.previous_position_array = new LimitedVector2Stack(5);
this.position_array.push(new Vector2(x, y));
this.previous_position_array.push(new Vector2(x, y));
this.pressed = true;
this.down = true;
this.released = false;
}
get position()
{
return this.region.invert_y(this.position_array.get_first());
}
get previous_position()
{
return this.region.invert_y(this.previous_position_array.get_first());
}
get html_NDC()
{
return this.region.transform_pos_to_NDC(this.region.invert_y(this.position_array.get_first()));
}
get NDC()
{
const ndc = this.region.transform_pos_to_NDC(this.position);
return ndc;
}
get NDC_delta()
{
return this.region.transform_dir_to_NDC(this.get_position_delta());
}
distance_to(pointer)
{
return this.position_array.average.distanceTo(pointer.position_array.average);
}
previous_distance_to(pointer)
{
return this.previous_position_array.average.distanceTo(pointer.previous_position_array.average);
}
set_position(x, y)
{
this.previous_position_array.push(this.position_array.get_first());
this.position_array.push(new Vector2(x, y));
}
reset_previous_position()
{
this.previous_position_array.push(this.position_array.get_first().clone());
this.position_array.push(this.position_array.get_first().clone());
}
get_position_delta()
{
return this.position.clone().sub(this.previous_position);
}
}
class MathUtilities

@@ -79,3 +347,3 @@ {

{
constructor()
constructor(region)
{

@@ -97,5 +365,15 @@ this.left_mouse_button_pressed = false;

this.pointer = new Pointer(9999, 0, 0, region);
this.pointer.pressed = false;
this.pointer.down = false;
this.pointer.released = false;
this.scroll_delta = 0;
}
get_primary_pointer()
{
return this.pointer;
}
get pointer_count()

@@ -122,7 +400,8 @@ {

{
this.pointer_pos.x = event.clientX;
this.pointer_pos.y = event.clientY;
// this.pointer_pos.x = event.clientX;
// this.pointer_pos.y = event.clientY;
this.pointer.set_position(event.clientX, event.clientY);
this.previous_pointer_pos.x = event.clientX;
this.previous_pointer_pos.y = event.clientY;
// this.previous_pointer_pos.x = event.clientX;
// this.previous_pointer_pos.y = event.clientY;

@@ -134,2 +413,4 @@ switch (event.button)

this.left_mouse_button_down = true;
this.pointer.pressed = true;
this.pointer.down = true;
break;

@@ -154,2 +435,4 @@ case 1:

this.left_mouse_button_down = false;
this.pointer.released = true;
this.pointer.down = false;
break;

@@ -169,4 +452,3 @@ case 1:

{
this.pointer_pos.x = event.clientX;
this.pointer_pos.y = event.clientY;
this.pointer.set_position(event.clientX, event.clientY);
}

@@ -185,2 +467,4 @@

this.left_mouse_button_released = true;
this.pointer.down = false;
this.pointer.released = true;
}

@@ -201,5 +485,3 @@ if (this.middle_mouse_button_down)

{
this.pointer_pos.x = event.clientX;
this.pointer_pos.y = event.clientY;
this.pointer.set_position(event.clientX, event.clientY);
if (OS$1.is_mac)

@@ -254,2 +536,5 @@ {

this.pointer.pressed = false;
this.pointer.released = false;
this.right_mouse_button_pressed = false;

@@ -268,6 +553,3 @@ this.right_mouse_button_released = false;

{
return {
x: this.pointer_pos.x - this.previous_pointer_pos.x,
y: this.pointer_pos.y - this.previous_pointer_pos.y
};
return this.pointer.get_position_delta();
}

@@ -277,3 +559,3 @@

{
return this.pointer_pos;
return this.pointer.position;
}

@@ -283,3 +565,3 @@

{
return this.pointer_pos_delta;
return this.pointer.get_position_delta();
}

@@ -289,252 +571,84 @@

{
this.previous_pointer_pos.x = this.pointer_pos.x;
this.previous_pointer_pos.y = this.pointer_pos.y;
this.pointer.reset_previous_position();
}
}
class Vector2
{
constructor(x = 0, y = 0)
get_primary_pointer_position()
{
this.x = x;
this.y = y;
return this.pointer.position;
}
set(x, y)
{
this.x = x;
this.y = y;
return this;
}
clone()
{
return new this.constructor(this.x, this.y);
}
copy(v)
{
this.x = v.x;
this.y = v.y;
return this;
}
add(v)
{
this.x += v.x;
this.y += v.y;
return this;
}
sub(v)
{
this.x -= v.x;
this.y -= v.y;
return this;
}
multiplyScalar(scalar)
{
this.x *= scalar;
this.y *= scalar;
return this;
}
divide(v)
{
this.x /= v.x;
this.y /= v.y;
return this;
}
divideScalar(scalar)
{
return this.multiplyScalar(1 / scalar);
}
dot(v)
{
return this.x * v.x + this.y * v.y;
}
cross(v)
{
return this.x * v.y - this.y * v.x;
}
lengthSq()
{
return this.x * this.x + this.y * this.y;
}
length()
{
return Math.sqrt(this.x * this.x + this.y * this.y);
}
normalize()
{
return this.divideScalar(this.length() || 1);
}
angle()
{
// computes the angle in radians with respect to the positive x-axis
const angle = Math.atan2(-this.y, -this.x) + Math.PI;
return angle;
}
distanceTo(v)
{
return Math.sqrt(this.distanceToSquared(v));
}
distanceToSquared(v)
{
const dx = this.x - v.x; const dy = this.y - v.y;
return dx * dx + dy * dy;
}
lerp(v, alpha)
{
this.x += (v.x - this.x) * alpha;
this.y += (v.y - this.y) * alpha;
return this;
}
}
Vector2.prototype.isVector2 = true;
class LimitedStack
class Region
{
constructor(max_size = 1)
constructor(region_element)
{
this.max_size = max_size;
this.region_element = region_element;
this.region_bounds = {
x: 0,
y: 0,
width: 1,
height: 1
};
this.array = [];
this.resize_observer = new ResizeObserver(this.update_region_bounds.bind(this));
this.resize_observer.observe(this.region_element);
}
get_first()
update_region_bounds()
{
return this.array[0];
}
const region_bounds = this.region_element.getBoundingClientRect();
length()
{
return this.array.length();
this.region_bounds.x = region_bounds.left;
this.region_bounds.y = region_bounds.top;
this.region_bounds.width = region_bounds.width;
this.region_bounds.height = region_bounds.height;
}
set_from_stack(stack)
check_for_legal_bounds()
{
for (let i = 0; i < this.array.length; i++)
if (this.region_bounds.width === 0 || this.region_bounds.height === 0)
{
this.array[i] = stack.array[i];
console.error('Cannot get normalized mouse position for target element due to the element having 0 width or height', this.dom_element, this.region_bounds);
}
}
push(elem)
invert_y(pos)
{
this.array.unshift(elem);
if (this.array.length > this.max_size)
{
this.array.pop();
}
const vec = new Vector2();
vec.copy(pos);
vec.y = this.region_bounds.height - vec.y;
return vec;
}
}
class LimitedVector2Stack extends LimitedStack
{
constructor(max_size = 1)
transform_pos_to_subregion(pos)
{
super(max_size);
const vec = new Vector2();
vec.copy(pos);
this.average = new Vector2();
}
vec.x -= this.region_bounds.x;
vec.y -= this.region_bounds.y;
set_from_stack(vector2_stack)
{
for (let i = 0; i < this.array.length; i++)
{
this.array[i].copy(vector2_stack.array[i]);
}
this.update_average();
return vec;
}
push(elem)
transform_pos_to_NDC(pos)
{
super.push(elem);
this.check_for_legal_bounds();
this.update_average();
}
const vec = this.transform_pos_to_subregion(pos);
update_average()
{
this.average.set(0, 0);
for (let i = 0; i < this.array.length; i++)
{
this.average.add(this.array[i]);
}
this.average.divideScalar(Math.max(1, this.array.length));
vec.x = (vec.x / this.region_bounds.width) * 2 - 1;
vec.y = (vec.y / this.region_bounds.height) * 2 - 1;
return vec;
}
}
class Pointer
{
constructor(id, x, y)
transform_dir_to_NDC(dir)
{
this.id = id;
const vec = new Vector2();
vec.copy(dir);
dir.x /= this.region_bounds.width;
dir.y /= this.region_bounds.height;
this.position_array = new LimitedVector2Stack(5);
this.previous_position_array = new LimitedVector2Stack(5);
this.position_array.push(new Vector2(x, y));
this.previous_position_array.push(new Vector2(x, y));
return dir;
}
get position()
{
return this.position_array.get_first();
}
get previous_position()
{
return this.previous_position_array.get_first();
}
distance_to(pointer)
{
return this.position_array.average.distanceTo(pointer.position_array.average);
}
previous_distance_to(pointer)
{
return this.previous_position_array.average.distanceTo(pointer.previous_position_array.average);
}
set_position(x, y)
{
this.previous_position_array.push(this.position_array.get_first());
this.position_array.push(new Vector2(x, y));
}
reset_previous_position()
{
this.previous_position_array.push(this.position_array.get_first().clone());
this.position_array.push(this.position_array.get_first().clone());
}
get_position_delta()
{
return this.position.clone().sub(this.previous_position);
}
}

@@ -544,4 +658,5 @@

{
constructor()
constructor(region)
{
this.region = region;
this.left_mouse_button_pressed = false;

@@ -556,3 +671,3 @@ this.left_mouse_button_down = false;

this.previous_primary_pointer_pos = { x: 0, y: 0 };
this.previous_primary_pointer_pos = new Vector2();

@@ -587,4 +702,16 @@ // this.update_pointer(7, 5, 5)

get pointer_pos()
get_primary_pointer()
{
if (this.pointers.length > 0)
{
return this.pointers[0];
}
else
{
return undefined;
}
}
get_primary_pointer_position()
{
const position = new Vector2();

@@ -701,4 +828,3 @@ position.x = this.previous_primary_pointer_pos.x;

{
// const is_primary = this.pointers.length === 0;
p = new Pointer(pointer_id, x, y);
p = new Pointer(pointer_id, x, y, this.region);
this.pointers.push(p);

@@ -798,2 +924,4 @@ }

const p = this.update_pointer(touch.identifier, touch.clientX, touch.clientY);
p.released = true;
p.down = false;

@@ -819,2 +947,3 @@ if (this.left_mouse_button_down && this.is_primary_pointer(p))

this.pointers[i].reset_previous_position();
this.pointers[i].pressed = false;
}

@@ -832,14 +961,8 @@ }

this.sub_region_element = sub_region_element === undefined ? dom_element : sub_region_element;
this.mouse_input_module = new MouseInputModule();
this.touch_input_module = new TouchInputModule();
this.region = new Region(this.sub_region_element);
this.mouse_input_module = new MouseInputModule(this.region);
this.touch_input_module = new TouchInputModule(this.region);
this.active_input_module = this.mouse_input_module;
this.region_bounds = {
x: 0,
y: 0,
width: 1,
height: 1
};
this.touch_cooldown = new Date() - 1000;

@@ -850,5 +973,2 @@

this.bind_events();
this.resize_observer = new ResizeObserver(this.update_region_bounds.bind(this));
this.resize_observer.observe(this.sub_region_element);
}

@@ -964,12 +1084,2 @@

update_region_bounds()
{
const region_bounds = this.sub_region_element.getBoundingClientRect();
this.region_bounds.x = region_bounds.left;
this.region_bounds.y = region_bounds.top;
this.region_bounds.width = region_bounds.width;
this.region_bounds.height = region_bounds.height;
}
mouse_input_allowed()

@@ -1041,36 +1151,2 @@ {

check_for_legal_bounds()
{
if (this.region_bounds.width === 0 || this.region_bounds.height === 0)
{
console.error('Cannot get normalized mouse position for target element due to the element having 0 width or height', this.dom_element, this.region_bounds);
}
}
invert_y(pos)
{
return {
x: pos.x,
y: this.region_bounds.height - pos.y
};
}
transform_pos_to_subregion(pos)
{
return {
x: pos.x - this.region_bounds.x,
y: pos.y - this.region_bounds.y
};
}
transform_pos_to_NDC(pos)
{
this.check_for_legal_bounds();
return {
x: (pos.x / this.region_bounds.width) * 2 - 1,
y: (pos.y / this.region_bounds.height) * 2 - 1
};
}
get scroll_delta()

@@ -1112,18 +1188,8 @@ {

{
return this.invert_y(this.transform_pos_to_subregion(this.active_input_module.pointer_pos));
return this.active_input_module.get_primary_pointer_position();
}
get html_pointer_pos()
{
return this.transform_pos_to_subregion(this.active_input_module.pointer_pos);
}
get pointer_pos_delta()
{
const pos_delta = this.active_input_module.pointer_pos_delta;
return {
x: pos_delta.x,
y: pos_delta.y * -1
};
return this.get_pointer_pos_delta(0);
}

@@ -1133,3 +1199,3 @@

{
return this.transform_pos_to_NDC(this.pointer_pos);
return this.active_input_module.get_primary_pointer().NDC;
}

@@ -1139,3 +1205,3 @@

{
return this.transform_pos_to_NDC(this.html_pointer_pos);
return this.active_input_module.get_primary_pointer().html_NDC;
}

@@ -1145,7 +1211,3 @@

{
this.check_for_legal_bounds();
return {
x: this.pointer_pos_delta.x / this.region_bounds.width,
y: this.pointer_pos_delta.y / this.region_bounds.height
};
return this.active_input_module.get_primary_pointer().NDC_delta;
}

@@ -1185,2 +1247,28 @@

get_pointer_pos()
{
return this.invert_y(this.active_input_module.get_primary_pointer_position(index));
}
get_pointer_pos_delta(index)
{
const pos_delta = this.active_input_module.get_pointer_pos_delta(index);
pos_delta.y *= -1;
return pos_delta;
}
get_pointer_NDC(index)
{
return this.transform_pos_to_NDC(this.get_pointer_pos());
}
get_pointer_NDC_delta(index)
{
this.check_for_legal_bounds();
const delta = this.get_pointer_pos_delta(index);
delta.x /= this.region_bounds.width;
delta.y /= this.region_bounds.height;
return delta;
}
dispose()

@@ -1187,0 +1275,0 @@ {

@@ -0,1 +1,269 @@

class Vector2
{
constructor(x = 0, y = 0)
{
this.x = x;
this.y = y;
}
set(x, y)
{
this.x = x;
this.y = y;
return this;
}
clone()
{
return new this.constructor(this.x, this.y);
}
copy(v)
{
this.x = v.x;
this.y = v.y;
return this;
}
add(v)
{
this.x += v.x;
this.y += v.y;
return this;
}
sub(v)
{
this.x -= v.x;
this.y -= v.y;
return this;
}
multiplyScalar(scalar)
{
this.x *= scalar;
this.y *= scalar;
return this;
}
divide(v)
{
this.x /= v.x;
this.y /= v.y;
return this;
}
divideScalar(scalar)
{
return this.multiplyScalar(1 / scalar);
}
dot(v)
{
return this.x * v.x + this.y * v.y;
}
cross(v)
{
return this.x * v.y - this.y * v.x;
}
lengthSq()
{
return this.x * this.x + this.y * this.y;
}
length()
{
return Math.sqrt(this.x * this.x + this.y * this.y);
}
normalize()
{
return this.divideScalar(this.length() || 1);
}
angle()
{
// computes the angle in radians with respect to the positive x-axis
const angle = Math.atan2(-this.y, -this.x) + Math.PI;
return angle;
}
distanceTo(v)
{
return Math.sqrt(this.distanceToSquared(v));
}
distanceToSquared(v)
{
const dx = this.x - v.x; const dy = this.y - v.y;
return dx * dx + dy * dy;
}
lerp(v, alpha)
{
this.x += (v.x - this.x) * alpha;
this.y += (v.y - this.y) * alpha;
return this;
}
}
Vector2.prototype.isVector2 = true;
class LimitedStack
{
constructor(max_size = 1)
{
this.max_size = max_size;
this.array = [];
}
get_first()
{
return this.array[0];
}
length()
{
return this.array.length();
}
set_from_stack(stack)
{
for (let i = 0; i < this.array.length; i++)
{
this.array[i] = stack.array[i];
}
}
push(elem)
{
this.array.unshift(elem);
if (this.array.length > this.max_size)
{
this.array.pop();
}
}
}
class LimitedVector2Stack extends LimitedStack
{
constructor(max_size = 1)
{
super(max_size);
this.average = new Vector2();
}
set_from_stack(vector2_stack)
{
for (let i = 0; i < this.array.length; i++)
{
this.array[i].copy(vector2_stack.array[i]);
}
this.update_average();
}
push(elem)
{
super.push(elem);
this.update_average();
}
update_average()
{
this.average.set(0, 0);
for (let i = 0; i < this.array.length; i++)
{
this.average.add(this.array[i]);
}
this.average.divideScalar(Math.max(1, this.array.length));
}
}
class Pointer
{
constructor(id, x, y, region)
{
this.region = region;
this.id = id;
this.position_array = new LimitedVector2Stack(5);
this.previous_position_array = new LimitedVector2Stack(5);
this.position_array.push(new Vector2(x, y));
this.previous_position_array.push(new Vector2(x, y));
this.pressed = true;
this.down = true;
this.released = false;
}
get position()
{
return this.region.invert_y(this.position_array.get_first());
}
get previous_position()
{
return this.region.invert_y(this.previous_position_array.get_first());
}
get html_NDC()
{
return this.region.transform_pos_to_NDC(this.region.invert_y(this.position_array.get_first()));
}
get NDC()
{
const ndc = this.region.transform_pos_to_NDC(this.position);
return ndc;
}
get NDC_delta()
{
return this.region.transform_dir_to_NDC(this.get_position_delta());
}
distance_to(pointer)
{
return this.position_array.average.distanceTo(pointer.position_array.average);
}
previous_distance_to(pointer)
{
return this.previous_position_array.average.distanceTo(pointer.previous_position_array.average);
}
set_position(x, y)
{
this.previous_position_array.push(this.position_array.get_first());
this.position_array.push(new Vector2(x, y));
}
reset_previous_position()
{
this.previous_position_array.push(this.position_array.get_first().clone());
this.position_array.push(this.position_array.get_first().clone());
}
get_position_delta()
{
return this.position.clone().sub(this.previous_position);
}
}
class MathUtilities

@@ -74,3 +342,3 @@ {

{
constructor()
constructor(region)
{

@@ -92,5 +360,15 @@ this.left_mouse_button_pressed = false;

this.pointer = new Pointer(9999, 0, 0, region);
this.pointer.pressed = false;
this.pointer.down = false;
this.pointer.released = false;
this.scroll_delta = 0;
}
get_primary_pointer()
{
return this.pointer;
}
get pointer_count()

@@ -117,7 +395,8 @@ {

{
this.pointer_pos.x = event.clientX;
this.pointer_pos.y = event.clientY;
// this.pointer_pos.x = event.clientX;
// this.pointer_pos.y = event.clientY;
this.pointer.set_position(event.clientX, event.clientY);
this.previous_pointer_pos.x = event.clientX;
this.previous_pointer_pos.y = event.clientY;
// this.previous_pointer_pos.x = event.clientX;
// this.previous_pointer_pos.y = event.clientY;

@@ -129,2 +408,4 @@ switch (event.button)

this.left_mouse_button_down = true;
this.pointer.pressed = true;
this.pointer.down = true;
break;

@@ -149,2 +430,4 @@ case 1:

this.left_mouse_button_down = false;
this.pointer.released = true;
this.pointer.down = false;
break;

@@ -164,4 +447,3 @@ case 1:

{
this.pointer_pos.x = event.clientX;
this.pointer_pos.y = event.clientY;
this.pointer.set_position(event.clientX, event.clientY);
}

@@ -180,2 +462,4 @@

this.left_mouse_button_released = true;
this.pointer.down = false;
this.pointer.released = true;
}

@@ -196,5 +480,3 @@ if (this.middle_mouse_button_down)

{
this.pointer_pos.x = event.clientX;
this.pointer_pos.y = event.clientY;
this.pointer.set_position(event.clientX, event.clientY);
if (OS$1.is_mac)

@@ -249,2 +531,5 @@ {

this.pointer.pressed = false;
this.pointer.released = false;
this.right_mouse_button_pressed = false;

@@ -263,6 +548,3 @@ this.right_mouse_button_released = false;

{
return {
x: this.pointer_pos.x - this.previous_pointer_pos.x,
y: this.pointer_pos.y - this.previous_pointer_pos.y
};
return this.pointer.get_position_delta();
}

@@ -272,3 +554,3 @@

{
return this.pointer_pos;
return this.pointer.position;
}

@@ -278,3 +560,3 @@

{
return this.pointer_pos_delta;
return this.pointer.get_position_delta();
}

@@ -284,252 +566,84 @@

{
this.previous_pointer_pos.x = this.pointer_pos.x;
this.previous_pointer_pos.y = this.pointer_pos.y;
this.pointer.reset_previous_position();
}
}
class Vector2
{
constructor(x = 0, y = 0)
get_primary_pointer_position()
{
this.x = x;
this.y = y;
return this.pointer.position;
}
set(x, y)
{
this.x = x;
this.y = y;
return this;
}
clone()
{
return new this.constructor(this.x, this.y);
}
copy(v)
{
this.x = v.x;
this.y = v.y;
return this;
}
add(v)
{
this.x += v.x;
this.y += v.y;
return this;
}
sub(v)
{
this.x -= v.x;
this.y -= v.y;
return this;
}
multiplyScalar(scalar)
{
this.x *= scalar;
this.y *= scalar;
return this;
}
divide(v)
{
this.x /= v.x;
this.y /= v.y;
return this;
}
divideScalar(scalar)
{
return this.multiplyScalar(1 / scalar);
}
dot(v)
{
return this.x * v.x + this.y * v.y;
}
cross(v)
{
return this.x * v.y - this.y * v.x;
}
lengthSq()
{
return this.x * this.x + this.y * this.y;
}
length()
{
return Math.sqrt(this.x * this.x + this.y * this.y);
}
normalize()
{
return this.divideScalar(this.length() || 1);
}
angle()
{
// computes the angle in radians with respect to the positive x-axis
const angle = Math.atan2(-this.y, -this.x) + Math.PI;
return angle;
}
distanceTo(v)
{
return Math.sqrt(this.distanceToSquared(v));
}
distanceToSquared(v)
{
const dx = this.x - v.x; const dy = this.y - v.y;
return dx * dx + dy * dy;
}
lerp(v, alpha)
{
this.x += (v.x - this.x) * alpha;
this.y += (v.y - this.y) * alpha;
return this;
}
}
Vector2.prototype.isVector2 = true;
class LimitedStack
class Region
{
constructor(max_size = 1)
constructor(region_element)
{
this.max_size = max_size;
this.region_element = region_element;
this.region_bounds = {
x: 0,
y: 0,
width: 1,
height: 1
};
this.array = [];
this.resize_observer = new ResizeObserver(this.update_region_bounds.bind(this));
this.resize_observer.observe(this.region_element);
}
get_first()
update_region_bounds()
{
return this.array[0];
}
const region_bounds = this.region_element.getBoundingClientRect();
length()
{
return this.array.length();
this.region_bounds.x = region_bounds.left;
this.region_bounds.y = region_bounds.top;
this.region_bounds.width = region_bounds.width;
this.region_bounds.height = region_bounds.height;
}
set_from_stack(stack)
check_for_legal_bounds()
{
for (let i = 0; i < this.array.length; i++)
if (this.region_bounds.width === 0 || this.region_bounds.height === 0)
{
this.array[i] = stack.array[i];
console.error('Cannot get normalized mouse position for target element due to the element having 0 width or height', this.dom_element, this.region_bounds);
}
}
push(elem)
invert_y(pos)
{
this.array.unshift(elem);
if (this.array.length > this.max_size)
{
this.array.pop();
}
const vec = new Vector2();
vec.copy(pos);
vec.y = this.region_bounds.height - vec.y;
return vec;
}
}
class LimitedVector2Stack extends LimitedStack
{
constructor(max_size = 1)
transform_pos_to_subregion(pos)
{
super(max_size);
const vec = new Vector2();
vec.copy(pos);
this.average = new Vector2();
}
vec.x -= this.region_bounds.x;
vec.y -= this.region_bounds.y;
set_from_stack(vector2_stack)
{
for (let i = 0; i < this.array.length; i++)
{
this.array[i].copy(vector2_stack.array[i]);
}
this.update_average();
return vec;
}
push(elem)
transform_pos_to_NDC(pos)
{
super.push(elem);
this.check_for_legal_bounds();
this.update_average();
}
const vec = this.transform_pos_to_subregion(pos);
update_average()
{
this.average.set(0, 0);
for (let i = 0; i < this.array.length; i++)
{
this.average.add(this.array[i]);
}
this.average.divideScalar(Math.max(1, this.array.length));
vec.x = (vec.x / this.region_bounds.width) * 2 - 1;
vec.y = (vec.y / this.region_bounds.height) * 2 - 1;
return vec;
}
}
class Pointer
{
constructor(id, x, y)
transform_dir_to_NDC(dir)
{
this.id = id;
const vec = new Vector2();
vec.copy(dir);
dir.x /= this.region_bounds.width;
dir.y /= this.region_bounds.height;
this.position_array = new LimitedVector2Stack(5);
this.previous_position_array = new LimitedVector2Stack(5);
this.position_array.push(new Vector2(x, y));
this.previous_position_array.push(new Vector2(x, y));
return dir;
}
get position()
{
return this.position_array.get_first();
}
get previous_position()
{
return this.previous_position_array.get_first();
}
distance_to(pointer)
{
return this.position_array.average.distanceTo(pointer.position_array.average);
}
previous_distance_to(pointer)
{
return this.previous_position_array.average.distanceTo(pointer.previous_position_array.average);
}
set_position(x, y)
{
this.previous_position_array.push(this.position_array.get_first());
this.position_array.push(new Vector2(x, y));
}
reset_previous_position()
{
this.previous_position_array.push(this.position_array.get_first().clone());
this.position_array.push(this.position_array.get_first().clone());
}
get_position_delta()
{
return this.position.clone().sub(this.previous_position);
}
}

@@ -539,4 +653,5 @@

{
constructor()
constructor(region)
{
this.region = region;
this.left_mouse_button_pressed = false;

@@ -551,3 +666,3 @@ this.left_mouse_button_down = false;

this.previous_primary_pointer_pos = { x: 0, y: 0 };
this.previous_primary_pointer_pos = new Vector2();

@@ -582,4 +697,16 @@ // this.update_pointer(7, 5, 5)

get pointer_pos()
get_primary_pointer()
{
if (this.pointers.length > 0)
{
return this.pointers[0];
}
else
{
return undefined;
}
}
get_primary_pointer_position()
{
const position = new Vector2();

@@ -696,4 +823,3 @@ position.x = this.previous_primary_pointer_pos.x;

{
// const is_primary = this.pointers.length === 0;
p = new Pointer(pointer_id, x, y);
p = new Pointer(pointer_id, x, y, this.region);
this.pointers.push(p);

@@ -793,2 +919,4 @@ }

const p = this.update_pointer(touch.identifier, touch.clientX, touch.clientY);
p.released = true;
p.down = false;

@@ -814,2 +942,3 @@ if (this.left_mouse_button_down && this.is_primary_pointer(p))

this.pointers[i].reset_previous_position();
this.pointers[i].pressed = false;
}

@@ -827,14 +956,8 @@ }

this.sub_region_element = sub_region_element === undefined ? dom_element : sub_region_element;
this.mouse_input_module = new MouseInputModule();
this.touch_input_module = new TouchInputModule();
this.region = new Region(this.sub_region_element);
this.mouse_input_module = new MouseInputModule(this.region);
this.touch_input_module = new TouchInputModule(this.region);
this.active_input_module = this.mouse_input_module;
this.region_bounds = {
x: 0,
y: 0,
width: 1,
height: 1
};
this.touch_cooldown = new Date() - 1000;

@@ -845,5 +968,2 @@

this.bind_events();
this.resize_observer = new ResizeObserver(this.update_region_bounds.bind(this));
this.resize_observer.observe(this.sub_region_element);
}

@@ -959,12 +1079,2 @@

update_region_bounds()
{
const region_bounds = this.sub_region_element.getBoundingClientRect();
this.region_bounds.x = region_bounds.left;
this.region_bounds.y = region_bounds.top;
this.region_bounds.width = region_bounds.width;
this.region_bounds.height = region_bounds.height;
}
mouse_input_allowed()

@@ -1036,36 +1146,2 @@ {

check_for_legal_bounds()
{
if (this.region_bounds.width === 0 || this.region_bounds.height === 0)
{
console.error('Cannot get normalized mouse position for target element due to the element having 0 width or height', this.dom_element, this.region_bounds);
}
}
invert_y(pos)
{
return {
x: pos.x,
y: this.region_bounds.height - pos.y
};
}
transform_pos_to_subregion(pos)
{
return {
x: pos.x - this.region_bounds.x,
y: pos.y - this.region_bounds.y
};
}
transform_pos_to_NDC(pos)
{
this.check_for_legal_bounds();
return {
x: (pos.x / this.region_bounds.width) * 2 - 1,
y: (pos.y / this.region_bounds.height) * 2 - 1
};
}
get scroll_delta()

@@ -1107,18 +1183,8 @@ {

{
return this.invert_y(this.transform_pos_to_subregion(this.active_input_module.pointer_pos));
return this.active_input_module.get_primary_pointer_position();
}
get html_pointer_pos()
{
return this.transform_pos_to_subregion(this.active_input_module.pointer_pos);
}
get pointer_pos_delta()
{
const pos_delta = this.active_input_module.pointer_pos_delta;
return {
x: pos_delta.x,
y: pos_delta.y * -1
};
return this.get_pointer_pos_delta(0);
}

@@ -1128,3 +1194,3 @@

{
return this.transform_pos_to_NDC(this.pointer_pos);
return this.active_input_module.get_primary_pointer().NDC;
}

@@ -1134,3 +1200,3 @@

{
return this.transform_pos_to_NDC(this.html_pointer_pos);
return this.active_input_module.get_primary_pointer().html_NDC;
}

@@ -1140,7 +1206,3 @@

{
this.check_for_legal_bounds();
return {
x: this.pointer_pos_delta.x / this.region_bounds.width,
y: this.pointer_pos_delta.y / this.region_bounds.height
};
return this.active_input_module.get_primary_pointer().NDC_delta;
}

@@ -1180,2 +1242,28 @@

get_pointer_pos()
{
return this.invert_y(this.active_input_module.get_primary_pointer_position(index));
}
get_pointer_pos_delta(index)
{
const pos_delta = this.active_input_module.get_pointer_pos_delta(index);
pos_delta.y *= -1;
return pos_delta;
}
get_pointer_NDC(index)
{
return this.transform_pos_to_NDC(this.get_pointer_pos());
}
get_pointer_NDC_delta(index)
{
this.check_for_legal_bounds();
const delta = this.get_pointer_pos_delta(index);
delta.x /= this.region_bounds.width;
delta.y /= this.region_bounds.height;
return delta;
}
dispose()

@@ -1182,0 +1270,0 @@ {

{
"name": "pit-js",
"version": "1.9.1",
"version": "2.0.0",
"description": "Pollable Input",

@@ -15,10 +15,2 @@ "module": "examples/build/PIT.module.js",

],
"devDependencies": {
"cross-var": "^1.1.0",
"rollup": "^2.44.0",
"rollup-plugin-sourcemaps": "^0.6.3",
"rollup-plugin-terser": "^7.0.2",
"standardx": "^7.0.0",
"typescript": "^4.4.3"
},
"scripts": {

@@ -32,3 +24,4 @@ "start": "rollup -w -c",

"fix-syntax": "standardx --fix || echo 'Done'",
"generate-types": "npx -p typescript tsc src/components/sdf_text/SDFTextBatch.js --declaration --allowJs --emitDeclarationOnly --outDir types"
"generate-types": "npx -p typescript tsc src/components/sdf_text/SDFTextBatch.js --declaration --allowJs --emitDeclarationOnly --outDir types",
"upload": "yarn build && npm publish && yarn create-tag"
},

@@ -52,3 +45,14 @@ "keywords": [

]
},
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
"devDependencies": {
"cross-var": "^1.1.0",
"rollup": "^2.44.0",
"rollup-plugin-sourcemaps": "^0.6.3",
"rollup-plugin-terser": "^7.0.2",
"standardx": "^7.0.0",
"typescript": "^4.4.3"
}
}
import MouseInputModule from './MouseInputModule';
import { Region } from './Region';
import TouchInputModule from './TouchInputModule';
import { Vector2 } from './Vector2';
import OS from './utilities/OS';

@@ -12,14 +14,8 @@ // import Logger from './utilities/Logger';

this.sub_region_element = sub_region_element === undefined ? dom_element : sub_region_element;
this.mouse_input_module = new MouseInputModule();
this.touch_input_module = new TouchInputModule();
this.region = new Region(this.sub_region_element);
this.mouse_input_module = new MouseInputModule(this.region);
this.touch_input_module = new TouchInputModule(this.region);
this.active_input_module = this.mouse_input_module;
this.region_bounds = {
x: 0,
y: 0,
width: 1,
height: 1
};
this.touch_cooldown = new Date() - 1000;

@@ -30,5 +26,2 @@

this.bind_events();
this.resize_observer = new ResizeObserver(this.update_region_bounds.bind(this));
this.resize_observer.observe(this.sub_region_element);
}

@@ -144,12 +137,2 @@

update_region_bounds()
{
const region_bounds = this.sub_region_element.getBoundingClientRect();
this.region_bounds.x = region_bounds.left;
this.region_bounds.y = region_bounds.top;
this.region_bounds.width = region_bounds.width;
this.region_bounds.height = region_bounds.height;
}
mouse_input_allowed()

@@ -221,36 +204,2 @@ {

check_for_legal_bounds()
{
if (this.region_bounds.width === 0 || this.region_bounds.height === 0)
{
console.error('Cannot get normalized mouse position for target element due to the element having 0 width or height', this.dom_element, this.region_bounds);
}
}
invert_y(pos)
{
return {
x: pos.x,
y: this.region_bounds.height - pos.y
};
}
transform_pos_to_subregion(pos)
{
return {
x: pos.x - this.region_bounds.x,
y: pos.y - this.region_bounds.y
};
}
transform_pos_to_NDC(pos)
{
this.check_for_legal_bounds();
return {
x: (pos.x / this.region_bounds.width) * 2 - 1,
y: (pos.y / this.region_bounds.height) * 2 - 1
};
}
get scroll_delta()

@@ -292,18 +241,8 @@ {

{
return this.invert_y(this.transform_pos_to_subregion(this.active_input_module.pointer_pos));
return this.active_input_module.get_primary_pointer_position();
}
get html_pointer_pos()
{
return this.transform_pos_to_subregion(this.active_input_module.pointer_pos);
}
get pointer_pos_delta()
{
const pos_delta = this.active_input_module.pointer_pos_delta;
return {
x: pos_delta.x,
y: pos_delta.y * -1
};
return this.get_pointer_pos_delta(0);
}

@@ -313,3 +252,3 @@

{
return this.transform_pos_to_NDC(this.pointer_pos);
return this.active_input_module.get_primary_pointer().NDC;
}

@@ -319,3 +258,3 @@

{
return this.transform_pos_to_NDC(this.html_pointer_pos);
return this.active_input_module.get_primary_pointer().html_NDC;
}

@@ -325,7 +264,3 @@

{
this.check_for_legal_bounds();
return {
x: this.pointer_pos_delta.x / this.region_bounds.width,
y: this.pointer_pos_delta.y / this.region_bounds.height
};
return this.active_input_module.get_primary_pointer().NDC_delta;
}

@@ -365,2 +300,28 @@

get_pointer_pos()
{
return this.invert_y(this.active_input_module.get_primary_pointer_position(index));
}
get_pointer_pos_delta(index)
{
const pos_delta = this.active_input_module.get_pointer_pos_delta(index);
pos_delta.y *= -1;
return pos_delta;
}
get_pointer_NDC(index)
{
return this.transform_pos_to_NDC(this.get_pointer_pos());
}
get_pointer_NDC_delta(index)
{
this.check_for_legal_bounds();
const delta = this.get_pointer_pos_delta(index);
delta.x /= this.region_bounds.width;
delta.y /= this.region_bounds.height;
return delta;
}
dispose()

@@ -367,0 +328,0 @@ {

@@ -0,1 +1,2 @@

import Pointer from './Pointer';
import MathUtilities from './utilities/MathUtilities';

@@ -5,3 +6,3 @@ import OS from './utilities/OS';

{
constructor()
constructor(region)
{

@@ -23,5 +24,15 @@ this.left_mouse_button_pressed = false;

this.pointer = new Pointer(9999, 0, 0, region);
this.pointer.pressed = false;
this.pointer.down = false;
this.pointer.released = false;
this.scroll_delta = 0;
}
get_primary_pointer()
{
return this.pointer;
}
get pointer_count()

@@ -48,7 +59,8 @@ {

{
this.pointer_pos.x = event.clientX;
this.pointer_pos.y = event.clientY;
// this.pointer_pos.x = event.clientX;
// this.pointer_pos.y = event.clientY;
this.pointer.set_position(event.clientX, event.clientY);
this.previous_pointer_pos.x = event.clientX;
this.previous_pointer_pos.y = event.clientY;
// this.previous_pointer_pos.x = event.clientX;
// this.previous_pointer_pos.y = event.clientY;

@@ -60,2 +72,4 @@ switch (event.button)

this.left_mouse_button_down = true;
this.pointer.pressed = true;
this.pointer.down = true;
break;

@@ -80,2 +94,4 @@ case 1:

this.left_mouse_button_down = false;
this.pointer.released = true;
this.pointer.down = false;
break;

@@ -95,4 +111,3 @@ case 1:

{
this.pointer_pos.x = event.clientX;
this.pointer_pos.y = event.clientY;
this.pointer.set_position(event.clientX, event.clientY);
}

@@ -111,2 +126,4 @@

this.left_mouse_button_released = true;
this.pointer.down = false;
this.pointer.released = true;
}

@@ -127,5 +144,3 @@ if (this.middle_mouse_button_down)

{
this.pointer_pos.x = event.clientX;
this.pointer_pos.y = event.clientY;
this.pointer.set_position(event.clientX, event.clientY);
if (OS.is_mac)

@@ -185,2 +200,5 @@ {

this.pointer.pressed = false;
this.pointer.released = false;
this.right_mouse_button_pressed = false;

@@ -199,6 +217,3 @@ this.right_mouse_button_released = false;

{
return {
x: this.pointer_pos.x - this.previous_pointer_pos.x,
y: this.pointer_pos.y - this.previous_pointer_pos.y
};
return this.pointer.get_position_delta();
}

@@ -208,3 +223,3 @@

{
return this.pointer_pos;
return this.pointer.position;
}

@@ -214,3 +229,3 @@

{
return this.pointer_pos_delta;
return this.pointer.get_position_delta();
}

@@ -220,5 +235,9 @@

{
this.previous_pointer_pos.x = this.pointer_pos.x;
this.previous_pointer_pos.y = this.pointer_pos.y;
this.pointer.reset_previous_position();
}
get_primary_pointer_position()
{
return this.pointer.position;
}
}

@@ -7,4 +7,5 @@ import { Vector2 } from './Vector2';

{
constructor(id, x, y)
constructor(id, x, y, region)
{
this.region = region;
this.id = id;

@@ -17,2 +18,6 @@

this.previous_position_array.push(new Vector2(x, y));
this.pressed = true;
this.down = true;
this.released = false;
}

@@ -22,3 +27,3 @@

{
return this.position_array.get_first();
return this.region.invert_y(this.position_array.get_first());
}

@@ -28,5 +33,21 @@

{
return this.previous_position_array.get_first();
return this.region.invert_y(this.previous_position_array.get_first());
}
get html_NDC()
{
return this.region.transform_pos_to_NDC(this.region.invert_y(this.position_array.get_first()));
}
get NDC()
{
const ndc = this.region.transform_pos_to_NDC(this.position);
return ndc;
}
get NDC_delta()
{
return this.region.transform_dir_to_NDC(this.get_position_delta());
}
distance_to(pointer)

@@ -33,0 +54,0 @@ {

@@ -7,4 +7,5 @@ import { Vector2 } from './Vector2';

{
constructor()
constructor(region)
{
this.region = region;
this.left_mouse_button_pressed = false;

@@ -19,3 +20,3 @@ this.left_mouse_button_down = false;

this.previous_primary_pointer_pos = { x: 0, y: 0 };
this.previous_primary_pointer_pos = new Vector2();

@@ -50,4 +51,16 @@ // this.update_pointer(7, 5, 5)

get pointer_pos()
get_primary_pointer()
{
if (this.pointers.length > 0)
{
return this.pointers[0];
}
else
{
return undefined;
}
}
get_primary_pointer_position()
{
const position = new Vector2();

@@ -164,4 +177,3 @@ position.x = this.previous_primary_pointer_pos.x;

{
// const is_primary = this.pointers.length === 0;
p = new Pointer(pointer_id, x, y);
p = new Pointer(pointer_id, x, y, this.region);
this.pointers.push(p);

@@ -261,2 +273,4 @@ }

const p = this.update_pointer(touch.identifier, touch.clientX, touch.clientY);
p.released = true;
p.down = false;

@@ -282,4 +296,5 @@ if (this.left_mouse_button_down && this.is_primary_pointer(p))

this.pointers[i].reset_previous_position();
this.pointers[i].pressed = false;
}
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc