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

Adding support for textures #105

Open
wants to merge 3 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added demos/textures/dice.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
102 changes: 102 additions & 0 deletions demos/textures/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />

<title>Textures</title>

<style>
html { height: 100%; }

body {
min-height: 100%;
margin: 0;
}

.illo {
display: block;
margin: 20px auto;
background: #FDB;
cursor: move;
}
.container {
margin: 0 5px;
}
.row {
display: flex;
align-items: center;
justify-content: center;
font-family: sans-serif;
text-align: center;
}
</style>

</head>
<body>

<div class="row">
<h1>Canvas</h1>
</div>
<div class="row">
<div class="container">
<h3>Gradient</h3>
<canvas class="illo" id="illo_coin" width="300" height="300"></canvas>
</div>

<div class="container">
<h3>Box</h3>
<canvas class="illo" id="illo_box" width="300" height="300"></canvas>
</div>

<div class="container">
<h3>Tetrahedron</h3>
<canvas class="illo" id="illo_tetrahedron" width="300" height="300"></canvas>
</div>
</div>

<div class="row">
<h1>SVG</h1>
</div>
<div class="row">
<div class="container">
<h3>Gradient</h3>
<svg class="illo" id="illo_coin_svg" width="300" height="300"></svg>
</div>

<div class="container">
<h3>Box</h3>
<svg class="illo" id="illo_box_svg" width="300" height="300"></svg>
</div>

<div class="container">
<h3>Tetrahedron</h3>
<svg class="illo" id="illo_tetrahedron_svg" width="300" height="300"></svg>
</div>
</div>

<script src="../../js/boilerplate.js"></script>
<script src="../../js/canvas-renderer.js"></script>
<script src="../../js/svg-renderer.js"></script>
<script src="../../js/vector.js"></script>
<script src="../../js/anchor.js"></script>
<script src="../../js/path-command.js"></script>
<script src="../../js/shape.js"></script>
<script src="../../js/ellipse.js"></script>
<script src="../../js/rect.js"></script>
<script src="../../js/rounded-rect.js"></script>
<script src="../../js/polygon.js"></script>
<script src="../../js/group.js"></script>
<script src="../../js/hemisphere.js"></script>
<script src="../../js/cylinder.js"></script>
<script src="../../js/cone.js"></script>
<script src="../../js/box.js"></script>
<script src="../../js/dragger.js"></script>
<script src="../../js/illustration.js"></script>

<script src="../../js/texture.js"></script>

<script src="textures.js"></script>

</body>
</html>
Binary file added demos/textures/tetrahedron.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
119 changes: 119 additions & 0 deletions demos/textures/textures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
function loadImage(url) {
return new Promise(r => { let i = new Image(); i.onload = (() => r(i)); i.src = url; });
}

function createIllustration(selector) {
// ----- variables ----- //
var isSpinning = true;

// ----- model ----- //
var illo = new Zdog.Illustration({
element: selector,
dragRotate: true,
zoom: 1,
onDragStart: function() {
isSpinning = false;
},
});

// ----- animate ----- //
function animate() {
illo.rotate.y += isSpinning ? 0.03 : 0;
illo.updateRenderGraph();
requestAnimationFrame( animate );
}
animate();
return illo;
}

function loadForBothRenderer(val, callback) {
callback("", val);
callback("_svg", val);
}

// Coin
loadForBothRenderer(0, suffix => {
new Zdog.Cylinder({
addTo: createIllustration("#illo_coin" + suffix),
diameter: 200,
length: 10,
color: "#000",
backface: new Zdog.Texture({
linearGrad:[0, 0, 100, 0],
colorStops:[0, '#833ab4', .5, '#fd1d1d', 1, '#fcb045'],
dst: [-100, -100, 200, 200],
src:[0, 0, 100, 100]}),
frontFace: new Zdog.Texture({
radialGrad:[50, 50, 0, 50, 50, 50],
colorStops:[0, '#3f5efb', .25, '#b471a3', .5, '#adb753', .75, '#5fd0d8', 1, '#fc466b'],
dst: [-100, -100, 200, 200],
src:[0, 0, 100, 100]}),
stroke: false,
});
});

loadImage("dice.png").then(img => loadForBothRenderer(img, (suffix, img) => {
new Zdog.Box({
addTo: createIllustration("#illo_box" + suffix),
width: 120,
height: 120,
depth: 120,
stroke: false,
color: '#F00',
frontFace: new Zdog.Texture({img: img, src:[0, 0, 200, 200], dst: [-60, -60, 120, 120]}),
rearFace: new Zdog.Texture({img: img, src:[0, 200, 200, 200], dst: [-60, -60, 120, 120]}),
leftFace: new Zdog.Texture({img: img, src:[200, 0, 200, 200], dst: [-60, -60, 120, 120]}),
rightFace: new Zdog.Texture({img: img, src:[200, 200, 200, 200], dst: [-60, -60, 120, 120]}),
topFace: new Zdog.Texture({img: img, src:[400, 0, 200, 200], dst: [-60, -60, 120, 120]}),
bottomFace: new Zdog.Texture({img: img, src:[400, 200, 200, 200], dst: [-60, -60, 120, 120]}),
});
}));


// Tetrahedron
loadImage("tetrahedron.png").then(img => loadForBothRenderer(img, (suffix, img) => {
var tetrahedron = new Zdog.Anchor({
addTo: createIllustration("#illo_tetrahedron" + suffix),
translate: { x: 0, y: 0 },
});

var radius = 80;
var deg_120 = Zdog.TAU / 3;

var depthFactor = radius * Math.sqrt(6) * 3 / 2;
var r = -depthFactor / 12;

var p1 = {x: 0, y : radius, z: r};
var p2 = {x: radius * Math.sin(deg_120), y : radius * Math.cos(deg_120), z: r};
var p3 = {x: radius * Math.sin(2 * deg_120), y : radius * Math.cos(2 * deg_120), z: r}
var p4 = {x: 0, y: 0, z: depthFactor / 4};

new Zdog.Shape({
path: [p1, p2, p3, p1],
addTo: tetrahedron,
stroke: 1,
color: new Zdog.Texture({img: img, src:[{x:10, y: 210}, {x:210, y: 210}, {x:110, y: 36.8}], dst: [p1, p2, p3]}),
fill: true,
});
new Zdog.Shape({
path: [p1, p2, p4, p1],
addTo: tetrahedron,
stroke: 1,
color: new Zdog.Texture({img: img, src:[{x:210, y: 210}, {x:410, y: 210}, {x:310, y: 36.8}], dst: [p1, p2, p4]}),
fill: true,
});
new Zdog.Shape({
path: [p1, p4, p3, p1],
addTo: tetrahedron,
stroke: 1,
color: new Zdog.Texture({img: img, src:[{x:410, y: 210}, {x:610, y: 210}, {x:510, y: 36.8}], dst: [p1, p4, p3]}),
fill: true,
});
new Zdog.Shape({
path: [p4, p2, p3, p4],
addTo: tetrahedron,
stroke: 1,
color: new Zdog.Texture({img: img, src:[{x:610, y: 210}, {x:810, y: 210}, {x:710, y: 36.8}], dst: [p4, p2, p3]}),
fill: true,
});
}));
7 changes: 7 additions & 0 deletions js/boilerplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ Zdog.easeInOut = function( alpha, power ) {
return isFirstHalf ? curve : 1 - curve;
};

Zdog.isColor = function(value) {
return (typeof value == 'string') || (value && value.isTexture);
}
Zdog.cloneColor = function(value) {
return (value && value.clone) ? value.clone() : value;
}

return Zdog;

} ) );
6 changes: 5 additions & 1 deletion js/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ Box.prototype.setFace = function( faceName, value ) {
}
// update & add face
var options = this.getFaceOptions( faceName );
options.color = typeof value == 'string' ? value : this.color;
if (utils.isColor(value)) {
options.color = value;
} else {
options.color = utils.cloneColor(this.color);
}

if ( rect ) {
// update previous
Expand Down
22 changes: 18 additions & 4 deletions js/canvas-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,31 @@ CanvasRenderer.stroke = function( ctx, elem, isStroke, color, lineWidth ) {
if ( !isStroke ) {
return;
}
ctx.strokeStyle = color;
ctx.lineWidth = lineWidth;
ctx.stroke();
if (color && color.getCanvasFill) {
ctx.save();
ctx.strokeStyle = color.getCanvasFill(ctx);
ctx.stroke();
ctx.restore();
} else {
ctx.strokeStyle = color;
ctx.stroke();
}
};

CanvasRenderer.fill = function( ctx, elem, isFill, color ) {
if ( !isFill ) {
return;
}
ctx.fillStyle = color;
ctx.fill();
if (color && color.getCanvasFill) {
ctx.save();
ctx.fillStyle = color.getCanvasFill(ctx);
ctx.fill();
ctx.restore();
} else {
ctx.fillStyle = color;
ctx.fill();
}
};

CanvasRenderer.end = function() {};
Expand Down
4 changes: 2 additions & 2 deletions js/cylinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ Cylinder.prototype.create = function( /* options */) {
color: this.color,
stroke: this.stroke,
fill: this.fill,
backface: this.frontFace || baseColor,
backface: utils.cloneColor(this.frontFace || baseColor),
visible: this.visible,
});
// back outside base
this.rearBase = this.group.rearBase = this.frontBase.copy({
translate: { z: -baseZ },
rotate: { y: 0 },
backface: baseColor,
backface: utils.cloneColor(baseColor),
});
};

Expand Down
6 changes: 4 additions & 2 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
require('./hemisphere'),
require('./cylinder'),
require('./cone'),
require('./box')
require('./box'),
require('./texture')
);
} else if ( typeof define == 'function' && define.amd ) {
/* globals define */ // AMD
Expand All @@ -33,7 +34,7 @@
/* eslint-disable max-params */
} )( this, function factory( Zdog, CanvasRenderer, SvgRenderer, Vector, Anchor,
Dragger, Illustration, PathCommand, Shape, Group, Rect, RoundedRect,
Ellipse, Polygon, Hemisphere, Cylinder, Cone, Box ) {
Ellipse, Polygon, Hemisphere, Cylinder, Cone, Box, Texture ) {
/* eslint-enable max-params */

Zdog.CanvasRenderer = CanvasRenderer;
Expand All @@ -53,6 +54,7 @@
Zdog.Cylinder = Cylinder;
Zdog.Cone = Cone;
Zdog.Box = Box;
Zdog.Texture = Texture;

return Zdog;
} );
20 changes: 16 additions & 4 deletions js/shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ Shape.prototype.reset = function() {
this.pathCommands.forEach( function( command ) {
command.reset();
} );

if (this.backface && this.backface.reset) {
this.backface.reset();
}
if (this.color && this.color.reset) {
this.color.reset();
}
};

Shape.prototype.transform = function( translation, rotation, scale ) {
Expand All @@ -106,6 +113,12 @@ Shape.prototype.transform = function( translation, rotation, scale ) {
this.children.forEach( function( child ) {
child.transform( translation, rotation, scale );
} );
if (this.backface && this.backface.transform) {
this.backface.transform( translation, rotation, scale );
}
if (this.color && this.color.transform) {
this.color.transform( translation, rotation, scale );
}
};

Shape.prototype.updateSortValue = function() {
Expand Down Expand Up @@ -153,17 +166,16 @@ Shape.prototype.render = function( ctx, renderer ) {

var TAU = utils.TAU;
// Safari does not render lines with no size, have to render circle instead
Shape.prototype.renderCanvasDot = function( ctx ) {
Shape.prototype.renderCanvasDot = function( ctx , renderer) {
var lineWidth = this.getLineWidth();
if ( !lineWidth ) {
return;
}
ctx.fillStyle = this.getRenderColor();
var point = this.pathCommands[0].endRenderPoint;
ctx.beginPath();
var radius = lineWidth/2;
ctx.arc( point.x, point.y, radius, 0, TAU );
ctx.fill();
renderer.fill(ctx, null, true, this.getRenderColor() );
};

Shape.prototype.getLineWidth = function() {
Expand All @@ -178,7 +190,7 @@ Shape.prototype.getLineWidth = function() {

Shape.prototype.getRenderColor = function() {
// use backface color if applicable
var isBackfaceColor = typeof this.backface == 'string' && this.isFacingBack;
var isBackfaceColor = utils.isColor(this.backface) && this.isFacingBack;
var color = isBackfaceColor ? this.backface : this.color;
return color;
};
Expand Down