Initial commit
This commit is contained in:
commit
b06258d4f0
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/node_modules/
|
||||||
|
*.log
|
1
dist/dwelibs.min.js
vendored
Normal file
1
dist/dwelibs.min.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
!function(t){var e={};function n(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)n.d(r,i,function(e){return t[e]}.bind(null,i));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){"use strict";window.DWE={};n(1),n(2),n(3)},function(t,e,n){"use strict";DWE.context=function(t){return t.star=function(e,n,r,i,o){var a=Math.PI/2*3,u=e,c=n,h=Math.PI/r;t.beginPath(),t.moveTo(e,n-i);for(var s=0;s<r;s++)u=e+Math.cos(a)*i,c=n+Math.sin(a)*i,t.lineTo(u,c),a+=h,u=e+Math.cos(a)*o,c=n+Math.sin(a)*o,t.lineTo(u,c),a+=h;t.lineTo(e,n-i),t.closePath()},t.fillStar=function(e,n,r,i,o){t.star(e,n,r,i,o),t.fill()},t.circle=function(e,n,r,i){i||(i=0),t.beginPath(),t.arc(e,n,r,i,2*Math.PI,!0),t.closePath()},t.fillCircle=function(e,n,r,i){t.circle(e,n,r,i),t.fill()},t}},function(t,e,n){"use strict";var r=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}();function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}DWE.Math={},DWE.Math.random=function(t,e){return Math.floor(Math.random()*e)+t},DWE.Math.lerp=function(t,e,n){return(1-n)*t+n*e},DWE.Math.reverseLerp=function(t,e,n){return 1/((e-t)*n)},DWE.Math.intersectsBox=function(t,e,n,r,i,o,a,u){return 2*Math.abs(t-i)<=n+a&&2*Math.abs(e-o)<=r+u},DWE.Math.intersects2Box=function(t,e){return 2*Math.abs(t.x-e.x)<=t.width+e.width&&2*Math.abs(t.y-e.y)<=t.height+e.height},DWE.Math.clamp=function(t,e,n){return Math.max(e,Math.min(n,t))},DWE.Math.Vector2=function(){function t(e,n){i(this,t),this.x=e,this.y=n}return r(t,[{key:"normalize",value:function(){var t=this.magnitude;this.x/=t,this.y/=t}},{key:"distance",value:function(t){if(!(t instanceof DWE.Math.Vector2))throw new Error("Distance function takes another Vector2 as an argument.");return Math.abs(Math.sqrt(Math.pow(t.x-this.x,2)+Math.pow(t.y-this.y,2)))}},{key:"magnitude",get:function(){return Math.abs(Math.sqrt(this.x*this.x+this.y*this.y))}}]),t}(),DWE.Math.Box2=function(){function t(e,n,r,o){i(this,t),this.x=e,this.y=n,this.width=r,this.height=o}return r(t,[{key:"intersectsBox",value:function(t){if(!(t instanceof DWE.Math.Box2))throw new Error("Intersection function takes another Box2 as an argument.");return DWE.Math.intersects2Box(this,t)}},{key:"intersectsCircle",value:function(t){if(!(t instanceof DWE.Math.Circle))throw new Error("Intersection function takes another Circle as an argument.");var e=new DWE.Math.Vector2(Math.abs(t.x-this.x),Math.abs(t.y-this.y));return!(e.x>this.width/2+t.r)&&(!(e.y>this.height/2+t.r)&&(e.x<=this.width/2||(e.y<=this.height/2||Math.pow(e.x-this.width/2,2)+Math.pow(e.y-this.height/2,2)<=Math.pow(t.r,2))))}}]),t}(),DWE.Math.Circle=function(){function t(e,n,r){i(this,t),this.x=e,this.y=n,this.r=r}return r(t,[{key:"radius",get:function(){return this.r}}]),t}()},function(t,e,n){"use strict";DWE.getMousePosition=function(t,e){var n=void 0,r=void 0;if(t.changedTouches){var i=t.changedTouches[0];i&&(t.pageX=i.pageX,t.pageY=i.pageY)}return t.pageX||t.pageY?(n=t.pageX,r=t.pageY):(n=t.clientX+document.body.scrollLeft+document.documentElement.scrollLeft,r=t.clientY+document.body.scrollTop+document.documentElement.scrollTop),{x:n-=e.offsetLeft,y:r-=e.offsetTop}}}]);
|
5822
package-lock.json
generated
Normal file
5822
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
24
package.json
Normal file
24
package.json
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"name": "dwelibs",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Common Canvas Functions for HTML5 Applications",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
|
"watch": "webpack -w --mode=development",
|
||||||
|
"build": "webpack -p"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"html5",
|
||||||
|
"canvas"
|
||||||
|
],
|
||||||
|
"author": "Evert \"Diamond\" Prants <evert@lunasqu.ee>",
|
||||||
|
"license": "CC0-1.0",
|
||||||
|
"devDependencies": {
|
||||||
|
"babel-core": "^6.26.3",
|
||||||
|
"babel-loader": "^7.1.5",
|
||||||
|
"babel-preset-env": "^1.7.0",
|
||||||
|
"webpack": "^4.16.5",
|
||||||
|
"webpack-command": "^0.4.1"
|
||||||
|
}
|
||||||
|
}
|
44
src/context.js
Normal file
44
src/context.js
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
|
||||||
|
DWE.context = function (ctx) {
|
||||||
|
ctx.star = function (cx, cy, spikes, outerRadius, innerRadius) {
|
||||||
|
var rot=Math.PI/2*3
|
||||||
|
var x=cx
|
||||||
|
var y=cy
|
||||||
|
var step=Math.PI/spikes
|
||||||
|
|
||||||
|
ctx.beginPath()
|
||||||
|
ctx.moveTo(cx,cy-outerRadius)
|
||||||
|
for(let i=0;i<spikes;i++){
|
||||||
|
x=cx+Math.cos(rot)*outerRadius
|
||||||
|
y=cy+Math.sin(rot)*outerRadius
|
||||||
|
ctx.lineTo(x,y)
|
||||||
|
rot+=step
|
||||||
|
|
||||||
|
x=cx+Math.cos(rot)*innerRadius
|
||||||
|
y=cy+Math.sin(rot)*innerRadius
|
||||||
|
ctx.lineTo(x,y)
|
||||||
|
rot+=step
|
||||||
|
}
|
||||||
|
ctx.lineTo(cx,cy-outerRadius)
|
||||||
|
ctx.closePath()
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.fillStar = function (cx, cy, spikes, outerRadius, innerRadius) {
|
||||||
|
ctx.star(cx, cy, spikes, outerRadius, innerRadius)
|
||||||
|
ctx.fill()
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.circle = function (x, y, r, d) {
|
||||||
|
if (!d) d = 0
|
||||||
|
ctx.beginPath()
|
||||||
|
ctx.arc(x, y, r, d, Math.PI * 2, true)
|
||||||
|
ctx.closePath()
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.fillCircle = function (x, y, r, d) {
|
||||||
|
ctx.circle(x, y, r, d)
|
||||||
|
ctx.fill()
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx
|
||||||
|
}
|
10
src/index.js
Normal file
10
src/index.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
var DWE = window.DWE = {}
|
||||||
|
|
||||||
|
// Context overrides
|
||||||
|
require('./context.js')
|
||||||
|
|
||||||
|
// Math functions
|
||||||
|
require('./math.js')
|
||||||
|
|
||||||
|
// Mouse helpers
|
||||||
|
require('./mouse.js')
|
97
src/math.js
Normal file
97
src/math.js
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
DWE.Math = {}
|
||||||
|
|
||||||
|
DWE.Math.random = function (i,j) {
|
||||||
|
return Math.floor(Math.random() * j) + i
|
||||||
|
}
|
||||||
|
|
||||||
|
DWE.Math.lerp = function (v0, v1, t) {
|
||||||
|
return (1 - t) * v0 + t * v1
|
||||||
|
}
|
||||||
|
|
||||||
|
DWE.Math.reverseLerp = function (v0, v1, t) {
|
||||||
|
return 1.0 / ((v1 - v0) * t)
|
||||||
|
}
|
||||||
|
|
||||||
|
DWE.Math.intersectsBox = function (ax, ay, aw, ah, bx, by, bw, bh) {
|
||||||
|
return (Math.abs(ax - bx) * 2 <= (aw + bw)) &&
|
||||||
|
(Math.abs(ay - by) * 2 <= (ah + bh))
|
||||||
|
}
|
||||||
|
|
||||||
|
DWE.Math.intersects2Box = function (a, b) {
|
||||||
|
return (Math.abs(a.x - b.x) * 2 <= (a.width + b.width)) &&
|
||||||
|
(Math.abs(a.y - b.y) * 2 <= (a.height + b.height))
|
||||||
|
}
|
||||||
|
|
||||||
|
DWE.Math.clamp = function (val, min, max) {
|
||||||
|
return Math.max(min, Math.min(max, val))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Two-dimensional vector
|
||||||
|
|
||||||
|
DWE.Math.Vector2 = class {
|
||||||
|
constructor (x, y) {
|
||||||
|
this.x = x
|
||||||
|
this.y = y
|
||||||
|
}
|
||||||
|
|
||||||
|
get magnitude () {
|
||||||
|
return Math.abs(Math.sqrt((this.x * this.x) + (this.y * this.y)))
|
||||||
|
}
|
||||||
|
|
||||||
|
normalize () {
|
||||||
|
var len = this.magnitude
|
||||||
|
this.x /= len
|
||||||
|
this.y /= len
|
||||||
|
}
|
||||||
|
|
||||||
|
distance (vec) {
|
||||||
|
if (!(vec instanceof DWE.Math.Vector2)) throw new Error('Distance function takes another Vector2 as an argument.')
|
||||||
|
return Math.abs(Math.sqrt(Math.pow(vec.x - this.x, 2) + Math.pow(vec.y - this.y, 2)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A box.
|
||||||
|
|
||||||
|
DWE.Math.Box2 = class {
|
||||||
|
constructor (x, y, w, h) {
|
||||||
|
this.x = x
|
||||||
|
this.y = y
|
||||||
|
this.width = w
|
||||||
|
this.height = h
|
||||||
|
}
|
||||||
|
|
||||||
|
intersectsBox (box) {
|
||||||
|
if (!(box instanceof DWE.Math.Box2)) throw new Error('Intersection function takes another Box2 as an argument.')
|
||||||
|
return DWE.Math.intersects2Box(this, box)
|
||||||
|
}
|
||||||
|
|
||||||
|
intersectsCircle (circle) {
|
||||||
|
if (!(circle instanceof DWE.Math.Circle)) throw new Error('Intersection function takes another Circle as an argument.')
|
||||||
|
var circleDistance = new DWE.Math.Vector2(Math.abs(circle.x - this.x), Math.abs(circle.y - this.y))
|
||||||
|
|
||||||
|
if (circleDistance.x > (this.width/2 + circle.r)) return false
|
||||||
|
if (circleDistance.y > (this.height/2 + circle.r)) return false
|
||||||
|
|
||||||
|
if (circleDistance.x <= (this.width/2)) return true
|
||||||
|
if (circleDistance.y <= (this.height/2)) return true
|
||||||
|
|
||||||
|
var cornerDistance_sq = Math.pow(circleDistance.x - this.width/2, 2) +
|
||||||
|
Math.pow(circleDistance.y - this.height/2, 2)
|
||||||
|
|
||||||
|
return (cornerDistance_sq <= Math.pow(circle.r, 2))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A circle.
|
||||||
|
|
||||||
|
DWE.Math.Circle = class {
|
||||||
|
constructor (x, y, r) {
|
||||||
|
this.x = x
|
||||||
|
this.y = y
|
||||||
|
this.r = r
|
||||||
|
}
|
||||||
|
|
||||||
|
get radius () {
|
||||||
|
return this.r
|
||||||
|
}
|
||||||
|
}
|
25
src/mouse.js
Normal file
25
src/mouse.js
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
DWE.getMousePosition = function (e, canvas) {
|
||||||
|
let x
|
||||||
|
let y
|
||||||
|
|
||||||
|
if (e.changedTouches) {
|
||||||
|
let touch = e.changedTouches[0]
|
||||||
|
if (touch) {
|
||||||
|
e.pageX = touch.pageX
|
||||||
|
e.pageY = touch.pageY
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (e.pageX || e.pageY) {
|
||||||
|
x = e.pageX
|
||||||
|
y = e.pageY
|
||||||
|
} else {
|
||||||
|
x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft
|
||||||
|
y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop
|
||||||
|
}
|
||||||
|
|
||||||
|
x -= canvas.offsetLeft
|
||||||
|
y -= canvas.offsetTop
|
||||||
|
|
||||||
|
return {x: x, y: y}
|
||||||
|
}
|
23
webpack.config.js
Normal file
23
webpack.config.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
const path = require('path')
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
entry: './src/index.js',
|
||||||
|
output: {
|
||||||
|
path: path.resolve(__dirname, 'dist'),
|
||||||
|
filename: 'dwelibs.min.js'
|
||||||
|
},
|
||||||
|
module: {
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
test: /\.js$/,
|
||||||
|
exclude: /(node_modules)/,
|
||||||
|
use: {
|
||||||
|
loader: 'babel-loader',
|
||||||
|
options: {
|
||||||
|
presets: ['env']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user