Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ var Buttons = (function () {

var refresh = function () {
for (var i = 0; i < elements.length; ++i) {
if (elements[i] === activeElement) {
elements[i].className = 'quality-selected';
} else {
elements[i].className = 'quality-unselected';
}
var qualityStatus = '';
qualityStatus = (elements[i] === activeElement) ? 'selected' : 'unselected';
elements[i].className = 'quality-'+qualityStatus;
}
};

Expand Down
Binary file added images/lock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/unlock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 12 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<script type="text/template" id="main">
<div id="ui">
<div id="clear-button" class="button">Clear</div><div id="save-button" class="button">Save</div>

<div class="control-label">Quality</div>
<div id="qualities"></div>
<div style="clear:both"></div>
Expand All @@ -38,7 +37,10 @@
</div>

<div id="footer">
<a href="http://david.li">david.li</a> | <a href="http://github.com/dli/paint">Source code</a>
<div class="canvasLockContainer">
<img id="canvasLockImage" src="images/unlock.png">
</div>
</span> | <a href="http://david.li">david.li</a> | <a href="http://github.com/dli/paint">Source code</a>
</div>
</script>

Expand All @@ -50,8 +52,6 @@
</script>

<div id="placeholder"></div>


<script src="wrappedgl.js"></script>
<script src="utilities.js"></script>
<script src="rectangle.js"></script>
Expand Down Expand Up @@ -93,6 +93,14 @@
document.getElementById('error').innerHTML = 'Unfortunately, your browser does not support WebGL floating point textures.';
}
}

// Event listener for the drag to resize canvas button
document.addEventListener("DOMContentLoaded", function() {
document.getElementById('canvasLockImage').addEventListener('click', function(){
painter.topggleCanvasResizeLock();
});
});

</script>

</body>
Expand Down
10 changes: 10 additions & 0 deletions paint.css
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,13 @@ body {
#backlink {
margin-top: 20px;
}
.canvasLockContainer{
display: inline-block;width: 13px;
}
#canvasLockImage{
position: absolute;
top: 0;
left: 0;
cursor: pointer;
width: 13px;
}
64 changes: 38 additions & 26 deletions paint.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var Paint = (function () {
var COLOR_PICKER_TOP = 467;

var RESIZING_RADIUS = 20;
var RESIZING_FEATHER_SIZE = 8; //in pixels
var RESIZING_FEATHER_SIZE = 8; //in pixels

//box shadow parameters
var BOX_SHADOW_SIGMA = 5.0;
Expand All @@ -92,6 +92,9 @@ var Paint = (function () {
var DIFFUSE_SCALE = 0.15;
var LIGHT_DIRECTION = [0, 1, 1];

//canvas resizing
var CANVAS_RESIZE = false;


function pascalRow (n) {
var line = [1];
Expand All @@ -108,10 +111,10 @@ var Paint = (function () {
//take the 1s off the ends
coefficients.shift();
coefficients.pop();

var normalizationFactor = 0;
for (var i = 0; i < coefficients.length; ++i) {
normalizationFactor += coefficients[i];
normalizationFactor += coefficients[i];
}

var shader = [
Expand Down Expand Up @@ -308,13 +311,13 @@ var Paint = (function () {
}).bind(this));



this.qualityButtons = new Buttons(document.getElementById('qualities'),
QUALITIES.map(function (q) { return q.name })
, INITIAL_QUALITY, (function (index) {
this.resolutionScale = QUALITIES[index].resolutionScale;
this.simulator.changeResolution(this.getPaintingResolutionWidth(), this.getPaintingResolutionHeight());
}).bind(this));
}).bind(this));


this.colorPicker = new ColorPicker(this, 'brushColorHSVA', wgl, canvas, shaderSources, COLOR_PICKER_LEFT, 0);
Expand All @@ -326,7 +329,7 @@ var Paint = (function () {
this.saveButton.addEventListener('click', this.save.bind(this));


this.clearButton = document.getElementById('clear-button');
this.clearButton = document.getElementById('clear-button');
this.clearButton.addEventListener('click', (function () {
this.simulator.clear();
}).bind(this));
Expand Down Expand Up @@ -359,7 +362,7 @@ var Paint = (function () {
this.onResize();

window.addEventListener('resize', this.onResize.bind(this));


this.mouseX = 0;
this.mouseY = 0;
Expand Down Expand Up @@ -397,7 +400,7 @@ var Paint = (function () {
//when we finish resizing, we then resize the simulator to match
this.newPaintingRectangle = null;


this.interactionState = InteractionMode.NONE;


Expand All @@ -424,8 +427,8 @@ var Paint = (function () {
var shadowDrawState = wgl.createDrawState()
.uniform2f('u_bottomLeft', rectangle.left, rectangle.bottom)
.uniform2f('u_topRight', rectangle.getRight(), rectangle.getTop())
.uniform1f('u_sigma', BOX_SHADOW_SIGMA)
.uniform1f('u_alpha', alpha)
.uniform1f('u_sigma', BOX_SHADOW_SIGMA)
.uniform1f('u_alpha', alpha)
.enable(wgl.BLEND)
.blendFunc(wgl.ONE, wgl.ONE_MINUS_SRC_ALPHA)
.useProgram(this.shadowProgram)
Expand Down Expand Up @@ -672,7 +675,7 @@ var Paint = (function () {

wgl.drawArrays(panelDrawState, wgl.TRIANGLE_STRIP, 0, 4);




//shadow for panel
Expand Down Expand Up @@ -779,19 +782,19 @@ var Paint = (function () {
this.paintingRectangle.getRight() - MIN_PAINTING_WIDTH);
this.newPaintingRectangle.width = this.paintingRectangle.left + this.paintingRectangle.width - this.newPaintingRectangle.left;
}

if (this.resizingSide === ResizingSide.RIGHT || this.resizingSide === ResizingSide.TOP_RIGHT || this.resizingSide === ResizingSide.BOTTOM_RIGHT) {
this.newPaintingRectangle.width = Utilities.clamp(mouseX - this.paintingRectangle.left, MIN_PAINTING_WIDTH, this.maxPaintingWidth);
}

if (this.resizingSide === ResizingSide.BOTTOM || this.resizingSide === ResizingSide.BOTTOM_LEFT || this.resizingSide === ResizingSide.BOTTOM_RIGHT) {
this.newPaintingRectangle.bottom = Utilities.clamp(mouseY,
this.paintingRectangle.getTop() - this.maxPaintingWidth,
this.paintingRectangle.getTop() - MIN_PAINTING_WIDTH);

this.newPaintingRectangle.height = this.paintingRectangle.bottom + this.paintingRectangle.height - this.newPaintingRectangle.bottom;
}

if (this.resizingSide === ResizingSide.TOP || this.resizingSide === ResizingSide.TOP_LEFT || this.resizingSide === ResizingSide.TOP_RIGHT) {
this.newPaintingRectangle.height = Utilities.clamp(mouseY - this.paintingRectangle.bottom, MIN_PAINTING_WIDTH, this.maxPaintingWidth);
}
Expand All @@ -804,49 +807,58 @@ var Paint = (function () {
this.mouseY = mouseY;
};

Paint.prototype.topggleCanvasResizeLock = function(){
// toggle drag to unlock canvas
CANVAS_RESIZE=!CANVAS_RESIZE;
var img = CANVAS_RESIZE ? 'lock' : 'unlock';
document.getElementById('canvasLockImage').src='images/'+img+'.png';
}

Paint.prototype.allowCanvasResize = function(i){
return !CANVAS_RESIZE ? i: '';
}

Paint.prototype.getResizingSide = function (mouseX, mouseY) { //the side we'd be resizing with the current mouse position
//we can resize if our perpendicular distance to an edge is less than RESIZING_RADIUS


if (Math.abs(mouseX - this.paintingRectangle.left) <= RESIZING_RADIUS && Math.abs(mouseY - this.paintingRectangle.getTop()) <= RESIZING_RADIUS) { //top left
return ResizingSide.TOP_LEFT;
return this.allowCanvasResize(ResizingSide.TOP_LEFT);
}

if (Math.abs(mouseX - this.paintingRectangle.getRight()) <= RESIZING_RADIUS && Math.abs(mouseY - this.paintingRectangle.getTop()) <= RESIZING_RADIUS) { //top right
return ResizingSide.TOP_RIGHT;
return this.allowCanvasResize(ResizingSide.TOP_RIGHT);
}

if (Math.abs(mouseX - this.paintingRectangle.left) <= RESIZING_RADIUS && Math.abs(mouseY - this.paintingRectangle.bottom) <= RESIZING_RADIUS) { //bottom left
return ResizingSide.BOTTOM_LEFT;
return this.allowCanvasResize(ResizingSide.BOTTOM_LEFT);
}

if (Math.abs(mouseX - this.paintingRectangle.getRight()) <= RESIZING_RADIUS && Math.abs(mouseY - this.paintingRectangle.bottom) <= RESIZING_RADIUS) { //bottom right
return ResizingSide.BOTTOM_RIGHT;
return this.allowCanvasResize(ResizingSide.BOTTOM_RIGHT);
}


if (mouseY > this.paintingRectangle.bottom && mouseY <= this.paintingRectangle.getTop()) { //left or right
if (Math.abs(mouseX - this.paintingRectangle.left) <= RESIZING_RADIUS) { //left
return ResizingSide.LEFT;
return this.allowCanvasResize(ResizingSide.LEFT);
} else if (Math.abs(mouseX - this.paintingRectangle.getRight()) <= RESIZING_RADIUS) { //right
return ResizingSide.RIGHT;
return this.allowCanvasResize(ResizingSide.RIGHT);
}
}

if (mouseX > this.paintingRectangle.left && mouseX <= this.paintingRectangle.getRight()) { //bottom or top
if (Math.abs(mouseY - this.paintingRectangle.bottom) <= RESIZING_RADIUS) { //bottom
return ResizingSide.BOTTOM;
return this.allowCanvasResize(ResizingSide.BOTTOM);
} else if (Math.abs(mouseY - this.paintingRectangle.getTop()) <= RESIZING_RADIUS) { //top
return ResizingSide.TOP;
return this.allowCanvasResize(ResizingSide.TOP);
}
}

return ResizingSide.NONE;
};

//what interaction mode would be triggered if we clicked with given mouse position
Paint.prototype.desiredInteractionMode = function (mouseX, mouseY) {
Paint.prototype.desiredInteractionMode = function (mouseX, mouseY) {
var mouseOverPanel = mouseX < PANEL_WIDTH && mouseY > this.canvas.height - PANEL_HEIGHT;

if (mouseOverPanel) {
Expand Down Expand Up @@ -907,7 +919,7 @@ var Paint = (function () {
if (this.resizingSide === ResizingSide.LEFT || this.resizingSide === ResizingSide.TOP_LEFT || this.resizingSide === ResizingSide.BOTTOM_LEFT) {
offsetX = (this.paintingRectangle.left - this.newPaintingRectangle.left) * this.resolutionScale;
}

if (this.resizingSide === ResizingSide.BOTTOM || this.resizingSide === ResizingSide.BOTTOM_LEFT || this.resizingSide === ResizingSide.BOTTOM_RIGHT) {
offsetY = (this.paintingRectangle.bottom - this.newPaintingRectangle.bottom) * this.resolutionScale;
}
Expand Down