Skip to content

Commit

Permalink
doc javascript rewrite and update
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Aug 20, 2017
1 parent bff7a53 commit 2ef0360
Show file tree
Hide file tree
Showing 16 changed files with 610 additions and 514 deletions.
6 changes: 3 additions & 3 deletions demo/basic/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ <h2>Basic demo</h2>
<strong id="counter"></strong>
<div id="canvas-container">
<canvas id="quadtree_canvas"></canvas>
<canvas id="layer_canvas" onclick="addElements()" oncontextmenu="init(); return false"
onmousemove="hoverMouse(event)" onmouseleave="unregisterMouse()"
ontouchmove="hoverMouse(event)"></canvas>
<canvas id="layer_canvas" onclick="demo.addElements()" oncontextmenu="demo.init(); return false"
onmousemove="demo.hoverMouse(event)" onmouseleave="demo.unregisterMouse()"
ontouchmove="demo.hoverMouse(event)"></canvas>
</div>
<script src="basic.js"></script>
</body>
Expand Down
227 changes: 120 additions & 107 deletions demo/basic/basic.js
Original file line number Diff line number Diff line change
@@ -1,122 +1,135 @@
var canvas = document.getElementById("quadtree_canvas")
var layer = document.getElementById("layer_canvas")
var container = document.getElementById("canvas-container")
var counter = document.getElementById("counter")
var ctx = canvas.getContext("2d")
var layerCtx = layer.getContext("2d")
var width = Math.min(container.clientWidth, window.innerWidth)
var height = width === window.innerWidth ? window.innerWidth : container.clientHeight
DELAY = 1
canvas.width = width
canvas.height = height
layer.width = width
layer.height = height
/* global window, document */

quadtreeColor = 'rgba(120, 144, 156, 0.1)'
eltColor = 'rgba(229, 57, 53, 1)'
oversizeColor = 'rgba(136, 14, 79, 1)'
collidingColor = '#F57F17'
window.demo = (function() {

eltSizeQuota = 25
eltDrawMult = 100
eltIncrement = 10
var canvas = document.getElementById("quadtree_canvas")
var layer = document.getElementById("layer_canvas")
var container = document.getElementById("canvas-container")
var counter = document.getElementById("counter")
var ctx = canvas.getContext("2d")
var layerCtx = layer.getContext("2d")
var width = Math.min(container.clientWidth, window.innerWidth)
var height = width === window.innerWidth ? window.innerWidth : container.clientHeight

document.addEventListener('DOMContentLoaded', function () {
init()
updateCanvas()
updateLayer()
})

var init = function(){
canvas.width = width
canvas.height = height
container.style.height = height+"px"
ctx.clearRect(0, 0, width, height)
ctx.lineWidth = 1
layer.width = width
layer.height = height
layerCtx.clearRect(0, 0, width, height)
layerCtx.lineWidth = 1
quadtree = new Quadtree({
width: width,
height: height
})
updateCounter()
}

var updateCounter = function(){
counter.innerHTML = quadtree.size + " elements"
}
var basicDemo = {
quadtreeColor: "rgba(120, 144, 156, 0.1)",
eltColor: "rgba(229, 57, 53, 1)",
oversizeColor: "rgba(136, 14, 79, 1)",
collidingColor: "#F57F17",
eltSizeQuota: 25,
eltDrawMult: 100,
eltIncrement: 10,
mousePos: null,
quadtree: null,

var randomizeElement = function(){
var squareSize = randomNb(0, Math.min(width, height) / eltSizeQuota)
return {
x: randomNb(0, width),
y: randomNb(0, height),
width: squareSize,
height: squareSize,
color: eltColor//"#"+((1<<24)*Math.random()|0).toString(16)
}
}
init: function() {
canvas.width = width
canvas.height = height
container.style.height = height + "px"
ctx.clearRect(0, 0, width, height)
ctx.lineWidth = 1
layer.width = width
layer.height = height
layerCtx.clearRect(0, 0, width, height)
layerCtx.lineWidth = 1
this.quadtree = new window.Quadtree({
width: width,
height: height
})
this.updateCounter()
},

var updateCanvas = function(){
ctx.clearRect(0, 0, width, height)
quadtree.visit(function(){
ctx.strokeStyle = quadtreeColor
drawQuadtree(this)
ctx.fillStyle = eltColor
for(i in this.contents)
drawSquare(this.contents[i], true)
ctx.fillStyle = oversizeColor
for(i in this.oversized)
drawSquare(this.oversized[i], true)
})
}
updateCounter: function() {
counter.innerHTML = this.quadtree.size + " elements"
},

var updateLayer = function(){
layerCtx.clearRect(0, 0, width, height)
if(window.mousePos){
layerCtx.strokeStyle = collidingColor
layerCtx.fillStyle = collidingColor
layerCtx.beginPath()
layerCtx.moveTo(0, mousePos.y)
layerCtx.lineTo(width, mousePos.y)
layerCtx.closePath()
layerCtx.stroke()
layerCtx.beginPath()
layerCtx.moveTo(mousePos.x, 0)
layerCtx.lineTo(mousePos.x, height)
layerCtx.closePath()
layerCtx.stroke()
quadtree.colliding(mousePos).forEach(function(elt){
drawSquare(elt, true, layerCtx)
})
}
}
randomizeElement: function() {
var squareSize = window.randomNb(0, Math.min(width, height) / this.eltSizeQuota)
return {
x: window.randomNb(0, width),
y: window.randomNb(0, height),
width: squareSize,
height: squareSize,
color: this.eltColor// "#"+((1<<24)*Math.random()|0).toString(16)
}
},

updateCanvas: function() {
var that = this
ctx.clearRect(0, 0, width, height)
this.quadtree.visit(function() {
ctx.strokeStyle = that.quadtreeColor
window.drawQuadtree(this, false, ctx)
ctx.fillStyle = that.eltColor
for(var i in this.contents)
window.drawSquare(this.contents[i], true, ctx)
ctx.fillStyle = that.oversizeColor
for(var j in this.oversized)
window.drawSquare(this.oversized[j], true, ctx)
})
},

var addElements = function(){
var elementArray = []
for(var i = 0; i < eltIncrement; i++)
elementArray.push(randomizeElement())
quadtree.pushAll(elementArray)
updateCanvas()
updateLayer()
updateCounter()
}
updateLayer: function() {
layerCtx.clearRect(0, 0, width, height)
if(this.mousePos) {
layerCtx.strokeStyle = this.collidingColor
layerCtx.fillStyle = this.collidingColor
layerCtx.beginPath()
layerCtx.moveTo(0, this.mousePos.y)
layerCtx.lineTo(width, this.mousePos.y)
layerCtx.closePath()
layerCtx.stroke()
layerCtx.beginPath()
layerCtx.moveTo(this.mousePos.x, 0)
layerCtx.lineTo(this.mousePos.x, height)
layerCtx.closePath()
layerCtx.stroke()
this.quadtree.colliding(this.mousePos).forEach(function(elt) {
window.drawSquare(elt, true, layerCtx)
})
}
},

var unregisterMouse = function(){
delete window.mousePos
updateLayer()
}
addElements: function(n) {
var elementArray = []
for(var i = 0; i < (n || this.eltIncrement); i++)
elementArray.push(this.randomizeElement())
this.quadtree.pushAll(elementArray)
this.updateCanvas()
this.updateLayer()
this.updateCounter()
},

var hoverMouse = function(event){
mousePos = {
x: event.offsetX || (event.offsetX === 0 ? 0 : event.changedTouches[0].clientX - event.target.getBoundingClientRect().left),
y: event.offsetY || (event.offsetY === 0 ? 0 : event.changedTouches[0].clientY - event.target.getBoundingClientRect().top)
unregisterMouse: function() {
delete this.mousePos
this.updateLayer()
},

hoverMouse: function(event) {
this.mousePos = {
x: event.offsetX || (event.offsetX === 0 ? 0 : event.changedTouches[0].clientX - event.target.getBoundingClientRect().left),
y: event.offsetY || (event.offsetY === 0 ? 0 : event.changedTouches[0].clientY - event.target.getBoundingClientRect().top)
}
if(this.mousePos.x < 0 || this.mousePos.y < 0)
return
event.stopPropagation()
event.preventDefault()
this.updateLayer()
}
}
if(mousePos.x < 0 || mousePos.y < 0)
return
event.stopPropagation()
event.preventDefault()
updateLayer()
}

document.addEventListener("DOMContentLoaded", function() {
basicDemo.init()
basicDemo.addElements(50)
basicDemo.updateCanvas()
basicDemo.updateLayer()
})

return basicDemo

}())
4 changes: 2 additions & 2 deletions demo/collisions/collisions.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ <h2>Collisions demo</h2>
</p>
<div>
<strong id="counter"></strong>
<button class="action" onclick="addElements()">Add elements</button>
<button class="action" onclick="init()">Reset</button>
<button class="action" onclick="demo.addElements()">Add elements</button>
<button class="action" onclick="demo.init()">Reset</button>
</div>
<div id="canvas-container">
<canvas id="quadtree_canvas"></canvas>
Expand Down
Loading

0 comments on commit 2ef0360

Please sign in to comment.