Skip to content

Commit

Permalink
Added spraygun
Browse files Browse the repository at this point in the history
  • Loading branch information
Hunter Loftis committed Mar 29, 2011
1 parent bb2e16a commit c678f7b
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 26 deletions.
Binary file added css/images/spraygun.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 11 additions & 18 deletions css/main.css
Expand Up @@ -209,36 +209,29 @@ ul {

/* Brush */

#brush {
#spraygun {
position: absolute;
top: 0;
left: 0;
width: 29px;
height: 928px;
width: 75px;
height: 716px;
}
#brush .brush {
#spraygun .brush {
position: absolute;
top: -5px;
top: -10px;
left: 0;
background: transparent url(images/paintbrush.png) top right no-repeat;
width: 29px;
height: 928px;
background: transparent url(images/spraygun.png) top center no-repeat;
width: 75px;
height: 716px;
}
#brush .shadow {
#spraygun .shadow {
position: absolute;
left: -24px;
top: 327px;
left: 0px;
top: 230px;
width: 80px;
height: 266px;
background: transparent url(images/shadow.png) bottom left no-repeat;
}
#brush.down .brush {
background-image: url(images/paintbrush_down.png);
top: 5px;
}
#brush.down .shadow {
left: -28px;
}

/* Marker */

Expand Down
10 changes: 6 additions & 4 deletions index.html
Expand Up @@ -8,17 +8,19 @@
<body>

<div id="wrap">

<div id="drawing">
<canvas id="artboard" width="750" height="500"></canvas>
<canvas id="workboard" width="750" height="500"></canvas>

<div id="pen" data-bind="visible: pen.visible, css: {down: pen.down}, style: {left: pen.left, top: pen.top, zIndex: pen.zIndex}">
<div class="shadow" data-bind="style: {webkitTransform: pen.shadowTransform, MozTransform: pen.shadowTransform, msTransform: pen.shadowTransform, transform: pen.shadowTransform}"></div>
<div class="brush" data-bind="style: {webkitTransform: pen.transform, MozTransform: pen.transform, msTransform: pen.transform, transform: pen.transform}"></div>
</div>

<div id="brush" data-bind="visible: inkbrush.visible, css: {down: inkbrush.down}, style: {left: inkbrush.left, top: inkbrush.top, zIndex: inkbrush.zIndex}">
<div class="shadow" data-bind="style: {webkitTransform: inkbrush.shadowTransform, MozTransform: inkbrush.shadowTransform, msTransform: inkbrush.shadowTransform, transform: inkbrush.shadowTransform}"></div>
<div class="brush" data-bind="style: {webkitTransform: inkbrush.transform, MozTransform: inkbrush.transform, msTransform: inkbrush.transform, transform: inkbrush.transform}"></div>
<div id="spraygun" data-bind="visible: spraygun.visible, css: {down: spraygun.down}, style: {left: spraygun.left, top: spraygun.top, zIndex: spraygun.zIndex}">
<div class="shadow" data-bind="style: {webkitTransform: spraygun.shadowTransform, MozTransform: spraygun.shadowTransform, msTransform: spraygun.shadowTransform, transform: spraygun.shadowTransform}"></div>
<div class="brush" data-bind="style: {webkitTransform: spraygun.transform, MozTransform: spraygun.transform, msTransform: spraygun.transform, transform: spraygun.transform}"></div>
</div>

<div id="marker" data-bind="visible: marker.visible, css: {down: marker.down}, style: {left: marker.left, top: marker.top, zIndex: marker.zIndex}">
Expand Down Expand Up @@ -53,7 +55,7 @@
<div id="toolbox">
<ul>
<li id="marker_holder" data-bind="css: {invisible: ViewModel.marker.active}, click: function() { ViewModel.activebrush(ViewModel.marker); }"></li>
<li id="inkbrush_holder" data-bind="css: {invisible: ViewModel.inkbrush.active}, click: function() { ViewModel.activebrush(ViewModel.inkbrush); }"></li>
<li id="spraygun_holder" data-bind="css: {invisible: ViewModel.spraygun.active}, click: function() { ViewModel.activebrush(ViewModel.spraygun); }"></li>
<li id="pen_holder" data-bind="css: {invisible: ViewModel.pen.active}, click: function() { ViewModel.activebrush(ViewModel.pen); }"></li>
</ul>
</div>
Expand Down
4 changes: 2 additions & 2 deletions js/main.js
Expand Up @@ -9,7 +9,7 @@
var mouse = new Mouse(),
activebrush = ko.observable(null),
pen = new Brush(mouse, 'pen', 'pen_holder', activebrush, 'drawing', 'inkRenderer', drawing)
inkbrush = new Brush(mouse, 'brush', 'inkbrush_holder', activebrush, 'drawing', 'waterRenderer', drawing),
spraygun = new Brush(mouse, 'spraygun', 'spraygun_holder', activebrush, 'drawing', 'waterRenderer', drawing),
marker = new Brush(mouse, 'marker', 'marker_holder', activebrush, 'drawing', 'markerRenderer', drawing),
palette = new Palette(drawing),
canvas = new Canvas('artboard', 'workboard'),
Expand Down Expand Up @@ -39,7 +39,7 @@

window.ViewModel = {
pen: pen,
inkbrush: inkbrush,
spraygun: spraygun,
marker: marker,
mouse: mouse,
activebrush: activebrush,
Expand Down
132 changes: 132 additions & 0 deletions js/renderers/charcoal.js
@@ -0,0 +1,132 @@
(function($, ko) {

var ns = 'waterRenderer';

function WaterRenderer(canvas, options) {
var self = this;

this.settings = {
spacing: 15,
scatter: 10,
fps: 25,
momentum: 0.05,
radius: 20,
spread: 2,
gravity: 3,
opacity: 0.02,
fade: 0.004,
color: [0,0,0]
};

$.extend(this.settings, options || {})

this.ctx = canvas.ctx;
this.position = [null, null]; // [from, to]
this.dripping = false;

this.debug = $('#workboard')[0].getContext('2d');

$(document).bind(ns + '.down', function(event, position) {
self.position = [{x: position.x, y: position.y}, {x: position.x, y: position.y}];
self.startDrip();
});

$(document).bind(ns + '.move', function(event, position) {
self.position.shift();
self.position.push({x: position.x, y: position.y});
});

$(document).bind(ns + '.up', function(event, position) {
self.stopDrip();
});
}

WaterRenderer.prototype = {

startDrip: function() {
if (this.dripping) return;
this.dripping = true;
var self = this;

// Update loop

(function update() {
if (self.dripping) {
var dx = self.position[1].x - self.position[0].x,
dy = self.position[1].y - self.position[0].y,
dist = Math.sqrt(dx * dx + dy * dy) || 1,
spacing = self.settings.spacing,
drops = dist / spacing,
ratio = spacing / dist;

for (var i = 0; i <= drops; i++) {
var pos = {
x: self.position[1].x - ratio * dx * i,
y: self.position[1].y - ratio * dy * i
},
vel = {x: dx, y: dy};
new Drop(self.ctx, pos, vel, self.settings);
}

window.setTimeout(update, 1000 / self.settings.fps);
}
})();
},

stopDrip: function() {
this.dripping = false;
}
}

window.WaterRenderer = WaterRenderer;

// Helper constructors

function Drop(ctx, position, velocity, settings) {
var self = this;

this.ctx = ctx;
this.pos = position;
this.vel = {x: velocity.x * settings.momentum, y: velocity.y * settings.momentum};
this.r = settings.radius;
this.settings = settings;
this.opacity = settings.opacity;
this.colorString = 'rgba(' + settings.color.join(',') + ',';

this.debug = $('#workboard')[0].getContext('2d');

window.setTimeout(function() {self.update();}, 1000 / settings.fps);
}

Drop.prototype = {
update: function() {
var self = this;

this.debug.clearRect(this.pos.x - 3, this.pos.y - 3, 6, 6);

this.pos.y += this.settings.gravity;
this.pos.x += this.vel.x;
this.pos.y += this.vel.y;
this.r += this.settings.spread;
this.opacity -= this.settings.fade;

if (this.opacity > this.settings.fade) {

this.ctx.save();
this.ctx.translate(this.pos.x, this.pos.y);
this.ctx.fillStyle = this.colorString + this.opacity + ')';
this.ctx.beginPath();
this.ctx.arc(0, 0, this.r, 0, 1.8 * Math.PI, false);
this.ctx.closePath();
this.ctx.fill();
this.ctx.restore();

this.debug.fillStyle = '#0ff';
this.debug.fillRect(this.pos.x - 2, this.pos.y - 2, 4, 4);

window.setTimeout(function() {self.update();}, 1000 / this.settings.fps);
}
}
}

})(jQuery, ko);
1 change: 0 additions & 1 deletion js/renderers/marker.js
Expand Up @@ -29,7 +29,6 @@
$(document).bind(ns + '.move', function(event, position) {
if (self.currentStroke.length > self.maxLength) {
self.imprint();
self.currentStroke = [];
}
self.currentStroke.push({x: position.x, y: position.y});
self.stroke();
Expand Down
2 changes: 1 addition & 1 deletion js/renderers/water.js
Expand Up @@ -93,7 +93,7 @@
this.opacity = settings.opacity;
this.colorString = 'rgba(' + settings.color.join(',') + ',';

this.debug = $('#debugboard')[0].getContext('2d');
this.debug = $('#workboard')[0].getContext('2d');

window.setTimeout(function() {self.update();}, 1000 / settings.fps);
}
Expand Down

0 comments on commit c678f7b

Please sign in to comment.