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 box-shadow rendering #1086

Closed
wants to merge 5 commits into from
Closed
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
3 changes: 3 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ module.exports = function(grunt) {
});

grunt.registerTask('webdriver', 'Browser render tests', function(browser, test) {
if (process.env.TRAVIS_PULL_REQUEST != "false") {
return;
}
var selenium = require("./tests/selenium.js");
var done = this.async();
var browsers = (browser) ? [grunt.config.get(this.name + "." + browser)] : _.values(grunt.config.get(this.name));
Expand Down
4 changes: 4 additions & 0 deletions src/nodeparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ NodeParser.prototype.paintElement = function(container) {
this.renderer.renderBackground(container, bounds, container.borders.borders.map(getWidth));
}, this);

this.renderer.mask(container.backgroundClip, function() {
this.renderer.renderShadows(container, container.borders.clip);
}, this, container);

this.renderer.clip(container.clip, function() {
this.renderer.renderBorders(container.borders.borders);
}, this);
Expand Down
8 changes: 8 additions & 0 deletions src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ Renderer.prototype.renderBackgroundColor = function(container, bounds) {
}
};

Renderer.prototype.renderShadows = function(container, shape) {
var boxShadow = container.css('boxShadow');
if (boxShadow !== 'none') {
var shadows = boxShadow.split(/,(?![^(]*\))/);
this.shadow(shape, shadows);
}
};

Renderer.prototype.renderBorders = function(borders) {
borders.forEach(this.renderBorder, this);
};
Expand Down
42 changes: 42 additions & 0 deletions src/renderers/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,38 @@ CanvasRenderer.prototype.circleStroke = function(left, top, size, color, stroke,
this.ctx.stroke();
};

CanvasRenderer.prototype.shadow = function(shape, shadows) {
var parseShadow = function(str) {
var propertyFilters = { color: /^(#|rgb|hsl|(?!(inset|initial|inherit))\D+)/i, inset: /^inset/i, px: /px$/i };
var pxPropertyNames = [ 'x', 'y', 'blur', 'spread' ];
var properties = str.split(/ (?![^(]*\))/);
var info = {};
for (var key in propertyFilters) {
info[key] = properties.filter(propertyFilters[key].test.bind(propertyFilters[key]));
info[key] = info[key].length === 0 ? null : info[key].length === 1 ? info[key][0] : info[key];
}
for (var i=0; i<info.px.length; i++) {
info[pxPropertyNames[i]] = parseInt(info.px[i]);
}
return info;
};
var drawShadow = function(shadow) {
var info = parseShadow(shadow);
if (!info.inset) {
context.shadowOffsetX = info.x;
Copy link

@NetroScript NetroScript Mar 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
context.shadowOffsetX = info.x;
if (info.spread > 0) {
for (var i = -1; i < 2; i += 2) {
for (var i2 = 1; i2 > -2; i2 -= 2) {
context.shadowOffsetX = info.x + i * info.spread;
context.shadowOffsetY = info.y + i2 * info.spread;
context.shadowColor = info.color;
context.shadowBlur = info.blur;
context.fill();
}
}
return;
}
context.shadowOffsetX = info.x;

When the spread is greater 0 you can draw the shape 4 times to simulate the spread. As long as the spread value is not bigger than double of the actual width / height it should work as intended (or better than not handling it at all).
But it is not possible to do negative spread with this method.

Edit: Additionally it might be good to use shadows.reverse().forEach(drawShadow, this); instead of shadows.forEach(drawShadow, this); on line 72, because the browser (or at least Chrome) draws the box-shadow in the opposite order.

Edit2: I forgot to think about opacity (because in my project I had no opacity / blur) so you can probably scratch all of that because it would only work with info.blur=0 and a color without an alpha value

context.shadowOffsetY = info.y;
context.shadowColor = info.color;
context.shadowBlur = info.blur;
context.fill();
}
};
var context = this.setFillStyle('white');
context.save();
this.shape(shape);
shadows.forEach(drawShadow, this);
context.restore();
};

CanvasRenderer.prototype.drawShape = function(shape, color) {
this.shape(shape);
this.setFillStyle(color).fill();
Expand Down Expand Up @@ -76,6 +108,16 @@ CanvasRenderer.prototype.clip = function(shapes, callback, context) {
this.ctx.restore();
};

CanvasRenderer.prototype.mask = function(shapes, callback, context, container) {
var borderClip = shapes[shapes.length-1];
if (borderClip && borderClip.length) {
var canvasBorderCCW = ["rect", this.canvas.width, 0, -this.canvas.width, this.canvas.height];
var maskShape = [canvasBorderCCW].concat(borderClip).concat([borderClip[0]]);
shapes = shapes.slice(0,-1).concat([maskShape]);
}
this.clip(shapes, callback, context, container);
};

CanvasRenderer.prototype.shape = function(shape) {
this.ctx.beginPath();
shape.forEach(function(point, index) {
Expand Down