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

LinearGradient support absolute offset #637

Open
wants to merge 2 commits 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
40 changes: 39 additions & 1 deletion src/graphic/Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,44 @@ function createLinearGradient(ctx, obj, rect) {
return canvasGradient;
}

function transformAbsoluteOffset(offset, absolute, obj, rect) {
var x = obj.x == null ? 0 : obj.x;
var x2 = obj.x2 == null ? 1 : obj.x2;
var y = obj.y == null ? 0 : obj.y;
var y2 = obj.y2 == null ? 0 : obj.y2;

if (!obj.global) {
x = x * rect.width + rect.x;
x2 = x2 * rect.width + rect.x;
y = y * rect.height + rect.y;
y2 = y2 * rect.height + rect.y;
}

// Fix NaN when rect is Infinity
x = isNaN(x) ? 0 : x;
x2 = isNaN(x2) ? 1 : x2;
y = isNaN(y) ? 0 : y;
y2 = isNaN(y2) ? 0 : y2;
var length = Math.sqrt(Math.pow(x2 - x, 2) + Math.pow(y2 - y, 2));

if (absolute === 'start' | absolute === 'end') {
if (length === 0) {
return 0;
}
if (isFinite(offset)) {
if (absolute === 'start') {
return Math.min(Math.max(offset / length, 0), 1);
}
else if (absolute === 'end') {
return Math.min(Math.max((length - offset) / length, 0), 1);
}
throw new Error('absolute must to be \'start\' or \'end\'' + offset);
}
throw new Error('offset need to be finite:' + offset);
}
return offset;
}

function createRadialGradient(ctx, obj, rect) {
var width = rect.width;
var height = rect.height;
Expand Down Expand Up @@ -477,7 +515,7 @@ Style.prototype = {
var colorStops = obj.colorStops;
for (var i = 0; i < colorStops.length; i++) {
canvasGradient.addColorStop(
colorStops[i].offset, colorStops[i].color
transformAbsoluteOffset(colorStops[i].offset, colorStops[i].absolute, obj, rect), colorStops[i].color
);
}
return canvasGradient;
Expand Down
58 changes: 58 additions & 0 deletions test/barGradient.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Bar Gradient</title>
<script src="../dist/zrender.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<script type="text/javascript">
var N = 50;
for (var k = 0; k < 50; k++) {
var div = document.createElement('div');
var width = 600;
var height = 300;
div.style.width = width + 'px';
div.style.height = height + 'px';
document.body.appendChild(div);
// 初始化zrender
var zr = zrender.init(div, {
renderer: 'canvas'
});

for (var i = 0; i < N; i++) {
var h = height * Math.random();
var barShape = new zrender.Rect({
shape: {
x: i * width / N,
y: height,
width: width / N,
height: 0
},
style: {
fill: new zrender.LinearGradient(0,0,0,1,[
{offset:0,color:"red"},
{offset:2,color:"red",absolute:"start"},
{offset:2,color:"transparent",absolute:"start"},
{offset:4,color:"transparent",absolute:"start"},
{offset:4,color:"blue",absolute:"start"},
{offset:2,color:"blue",absolute:"end"},
{offset:2,color:"green",absolute:"end"},
{offset:1,color:"green"}
])
},
});
zr.add(barShape);

barShape.animateTo({
shape: {
height: h,
y: height - h
}
}, 500);
}
}
</script>
</body>
</html>