Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fill plugin #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
75 changes: 74 additions & 1 deletion build/darkroom.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,549 changes: 1,548 additions & 1 deletion build/darkroom.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ gulp.task('scripts', function () {
srcDir + '/js/core/darkroom.js',
srcDir + '/js/core/*.js',
// srcDir + '/js/plugins/*.js',
srcDir + '/js/plugins/darkroom.select.js',
srcDir + '/js/plugins/darkroom.history.js',
srcDir + '/js/plugins/darkroom.rotate.js',
srcDir + '/js/plugins/darkroom.crop.js',
srcDir + '/js/plugins/darkroom.fill.js',
srcDir + '/js/plugins/darkroom.save.js',
];

Expand Down
39 changes: 39 additions & 0 deletions lib/icons/fill.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
134 changes: 128 additions & 6 deletions lib/js/core/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
(function() {
'use strict';

Darkroom.Utils = {
extend: extend,
computeImageViewPort: computeImageViewPort,
};


// Utility method to easily extend objects.
function extend(b, a) {
var prop;
Expand All @@ -28,4 +22,132 @@ function computeImageViewPort(image) {
}
}

var SelectZone = fabric.util.createClass(fabric.Rect, {
_render: function(ctx) {
this.callSuper('_render', ctx);

var canvas = ctx.canvas;
var dashWidth = 7;

// Set original scale
var flipX = this.flipX ? -1 : 1;
var flipY = this.flipY ? -1 : 1;
var scaleX = flipX / this.scaleX;
var scaleY = flipY / this.scaleY;

ctx.scale(scaleX, scaleY);

// Overlay rendering
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
this._renderOverlay(ctx);

// Set dashed borders
if (ctx.setLineDash !== undefined)
ctx.setLineDash([dashWidth, dashWidth]);
else if (ctx.mozDash !== undefined)
ctx.mozDash = [dashWidth, dashWidth];

// First lines rendering with black
ctx.strokeStyle = 'rgba(0, 0, 0, 0.2)';
this._renderBorders(ctx);
this._renderGrid(ctx);

// Re render lines in white
ctx.lineDashOffset = dashWidth;
ctx.strokeStyle = 'rgba(255, 255, 255, 0.4)';
this._renderBorders(ctx);
this._renderGrid(ctx);

// Reset scale
ctx.scale(1/scaleX, 1/scaleY);
},

_renderOverlay: function(ctx) {
var canvas = ctx.canvas;

//
// x0 x1 x2 x3
// y0 +------------------------+
// |\\\\\\\\\\\\\\\\\\\\\\\\|
// |\\\\\\\\\\\\\\\\\\\\\\\\|
// y1 +------+---------+-------+
// |\\\\\\| |\\\\\\\|
// |\\\\\\| 0 |\\\\\\\|
// |\\\\\\| |\\\\\\\|
// y2 +------+---------+-------+
// |\\\\\\\\\\\\\\\\\\\\\\\\|
// |\\\\\\\\\\\\\\\\\\\\\\\\|
// y3 +------------------------+
//

var x0 = Math.ceil(-this.getWidth() / 2 - this.getLeft());
var x1 = Math.ceil(-this.getWidth() / 2);
var x2 = Math.ceil(this.getWidth() / 2);
var x3 = Math.ceil(this.getWidth() / 2 + (canvas.width - this.getWidth() - this.getLeft()));

var y0 = Math.ceil(-this.getHeight() / 2 - this.getTop());
var y1 = Math.ceil(-this.getHeight() / 2);
var y2 = Math.ceil(this.getHeight() / 2);
var y3 = Math.ceil(this.getHeight() / 2 + (canvas.height - this.getHeight() - this.getTop()));

ctx.beginPath();

// Draw outer rectangle.
// Numbers are +/-1 so that overlay edges don't get blurry.
ctx.moveTo(x0 - 1, y0 - 1);
ctx.lineTo(x3 + 1, y0 - 1);
ctx.lineTo(x3 + 1, y3 + 1);
ctx.lineTo(x0 - 1, y3 - 1);
ctx.lineTo(x0 - 1, y0 - 1);
ctx.closePath();

// Draw inner rectangle.
ctx.moveTo(x1, y1);
ctx.lineTo(x1, y2);
ctx.lineTo(x2, y2);
ctx.lineTo(x2, y1);
ctx.lineTo(x1, y1);

ctx.closePath();
ctx.fill();
},

_renderBorders: function(ctx) {
ctx.beginPath();
ctx.moveTo(-this.getWidth()/2, -this.getHeight()/2); // upper left
ctx.lineTo(this.getWidth()/2, -this.getHeight()/2); // upper right
ctx.lineTo(this.getWidth()/2, this.getHeight()/2); // down right
ctx.lineTo(-this.getWidth()/2, this.getHeight()/2); // down left
ctx.lineTo(-this.getWidth()/2, -this.getHeight()/2); // upper left
ctx.stroke();
},

_renderGrid: function(ctx) {
// Vertical lines
ctx.beginPath();
ctx.moveTo(-this.getWidth()/2 + 1/3 * this.getWidth(), -this.getHeight()/2);
ctx.lineTo(-this.getWidth()/2 + 1/3 * this.getWidth(), this.getHeight()/2);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(-this.getWidth()/2 + 2/3 * this.getWidth(), -this.getHeight()/2);
ctx.lineTo(-this.getWidth()/2 + 2/3 * this.getWidth(), this.getHeight()/2);
ctx.stroke();
// Horizontal lines
ctx.beginPath();
ctx.moveTo(-this.getWidth()/2, -this.getHeight()/2 + 1/3 * this.getHeight());
ctx.lineTo(this.getWidth()/2, -this.getHeight()/2 + 1/3 * this.getHeight());
ctx.stroke();
ctx.beginPath();
ctx.moveTo(-this.getWidth()/2, -this.getHeight()/2 + 2/3 * this.getHeight());
ctx.lineTo(this.getWidth()/2, -this.getHeight()/2 + 2/3 * this.getHeight());
ctx.stroke();
}
});

Darkroom.Utils = {
extend: extend,
computeImageViewPort: computeImageViewPort,
SelectZone: SelectZone
};

})();
Loading