Finalized triangle gradients experiment.

master
ben-burlingham 10 years ago
parent fa2bc67bce
commit b01d0d494a
  1. 155
      gradients-v1.js
  2. 175
      index.html

@ -0,0 +1,155 @@
'use strict';
/**
*
*/
function Point(x, y, name, hex) {
if (typeof x !== typeof 1) {
throw new Error("x = " + x + " must be of type 'number'; it is currently of type '" + (typeof x) + "'.");
}
if (typeof y !== typeof 1) {
throw new Error("y = " + y + " must be of type 'number'; it is currently of type '" + (typeof y) + "'.");
}
if (x < 0 || y < 0) {
throw new Error("X and Y must be greater than 0.");
}
this.x = x;
this.y = y;
this.name = name;
this.hex = hex;
}
/**
*
*/
function Triangle(a, b, c) {
if (!(a instanceof Point) || !(b instanceof Point) || !(c instanceof Point)) {
throw new Error("All three arguments must be of type Point.");
}
this.a = a;
this.b = b;
this.c = c;
}
/**
*
*/
Triangle.prototype = {
/**
*
*/
findMidpoint: function(p1, p2) {
var minX = Math.min(p1.x, p2.x);
var maxX = Math.max(p1.x, p2.x);
var minY = Math.min(p1.y, p2.y);
var maxY = Math.max(p1.y, p2.y);
var midX = minX + ((maxX - minX) / 2);
var midY = minY + ((maxY - minY) / 2);
return new Point(midX, midY);
},
/**
*
*/
buildGradient: function(p) {
var opp1, opp2;
if (p === this.a) {
opp1 = this.b;
opp2 = this.c;
}
if (p === this.b) {
opp1 = this.a;
opp2 = this.c;
}
if (p === this.c) {
opp1 = this.a;
opp2 = this.b;
}
var fill = d3.select(document.createElementNS(d3.ns.prefix.svg, "linearGradient"))
.attr("gradientUnits", "userSpaceOnUse")
.attr("id", `fill${p.name}`)
.attr("x1", p.x)
.attr("y1", p.y)
.attr("x2", this.findMidpoint(opp1, opp2).x)
.attr("y2", this.findMidpoint(opp1, opp2).y)
fill.append("stop")
.attr("offset", "0%")
.attr("stop-color", p.hex)
fill.append("stop")
.attr("offset", "100%")
.attr("stop-color", "#fff")
.attr("stop-opacity", "0")
return function() {
return fill.node();
};
},
/**
*
*/
buildPath: function(p) {
var path = d3.select(document.createElementNS(d3.ns.prefix.svg, "path"))
.attr("d", `M${this.a.x},${this.a.y} L${this.b.x},${this.b.y} L${this.c.x},${this.c.y} Z`)
.attr("fill", `url(#fill${p.name})`)
return function() {
return path.node();
};
}
};
/**
*
*/
(function() {
var svg = d3.select("body").append("svg")
.attr("width", 500)
.attr("height", 500);
var defs = svg.append("defs");
var points = [
[
new Point(100, 0, 'A1', '#f00'),
new Point(200, 200, 'B1', '#0f0'),
new Point(0, 200, 'C1', '#00f')
],
[
new Point(100, 0, 'A2', '#f00'),
new Point(200, 200, 'B2', '#0f0'),
new Point(300, 0, 'C2', '#00f')
],
[
new Point(300, 300, 'A3', '#f00'),
new Point(300, 0, 'C3', '#00f'),
new Point(200, 200, 'B3', '#0f0'),
],
];
for (var i = 0, len = points.length; i < len; i++) {
var T = new Triangle(points[i][0], points[i][1], points[i][2]);
defs.append(T.buildGradient(points[i][0]));
defs.append(T.buildGradient(points[i][1]));
defs.append(T.buildGradient(points[i][2]));
svg.append(T.buildPath(points[i][0]));
svg.append(T.buildPath(points[i][1]));
svg.append(T.buildPath(points[i][2]));
}
}());

@ -55,156 +55,63 @@
svg {
/*background: #098347;*/
margin:100px;
}
</style>
</head>
<body>
<!-- <ul>
<li>&#10003; Render any map</li>
<li>&#10003; Finish Bostock tutorial</li>
<li>&#9744; Render the ocean map</li>
<li>&#9744; Extract only CA coastline from ocean map</li>
<li>&#9744; Find how to render something at a point</li>
<li>&#9744; Find how to draw a flat line</li>
<li>&#9744; Get feed of ocean temperatures</li>
http://www.ndbc.noaa.gov/data/realtime2/46237.txt (last 26? days)
http://www.ndbc.noaa.gov/station_history.php?station=46237 (historical)
http://cdip.ucsd.edu/?nav=historic&sub=data&stn=142&stream=p1
http://cdip.ucsd.edu/?nav=documents&sub=index&units=metric&tz=UTC&pub=public&map_stati=1,2,3&xitem=arch
<li>&#9744; Get locations of CA buoys</li>
</ul> -->
<svg width='500px' height='500px' id='svg'>
<defs>
<linearGradient id="fillR" gradientUnits="userSpaceOnUse" x1="200" y1="200" x2="0" y2="0">
<stop offset="0%" stop-color="#f00" />
<stop offset="100%" stop-color="#fff" stop-opacity="0" />
</linearGradient>
<linearGradient id="fillG" gradientUnits="userSpaceOnUse" x1="100" y1="0" x2="100" y2="200">
<stop offset="0%" stop-color="#0f0" />
<stop offset="100%" stop-color="#fff" stop-opacity="0" />
</linearGradient>
<linearGradient id="fillB" gradientUnits="userSpaceOnUse" x1="0" y1="200" x2="150" y2="100">
<stop offset="0%" stop-color="#00f" />
<stop offset="100%" stop-color="#fff" stop-opacity="0" />
</linearGradient>
<filter id="triblend">
<feImage xlink:href="#pathA" result="layerA" x="0" y="0" />
<feComposite in="layerA" in2="SourceGraphic" operator="arithmetic" k1="0" k2="1.0" k3="1.0" k4="0" />
</filter>
<!-- <script src="d3.min.js"></script> -->
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script src="gradients-v1.js"></script>
<!-- <script src="gradients-v2.js"></script> -->
<!-- UK complete but needs some minor debugging <script src="uk.js"></script> -->
<!-- <script src="ocean.js"></script> -->
<!-- ORIGINAL -->
<!-- <svg viewBox="0 0 300px 300px">
<defs>
<linearGradient id="fillR" gradientUnits="userSpaceOnUse" x1="50" y1="0" x2="50" y2="86">
<stop offset="0%" stop-color="#FF0000"/>
<stop offset="100%" stop-color="" />
</linearGradient>
<linearGradient id="fillG" gradientUnits="userSpaceOnUse" x1="0" y1="86" x2="75" y2="43">
<stop offset="0%" stop-color="#00FF00"/>
<stop offset="100%" stop-color="" />
</linearGradient>
<linearGradient id="fillA" gradientUnits="userSpaceOnUse" x1="100" y1="86" x2="25" y2="43">
<stop offset="0%" stop-color="#0000FF"/>
<stop offset="100%" stop-color="" />
</linearGradient>
<path id="pathA-1" d="M 50,0 L 0,86 100,86 Z" fill="url(#fillR)"/>
<path id="pathB-1" d="M 50,0 L 0,86 100,86 Z" fill="url(#fillG)"/>
<filter id="Default">
<feImage xlink:href="#pathA-1" result="layerA" x="0" y="0" />
<feImage xlink:href="#pathB-1" result="layerB" x="0" y="0" />
<feComposite in="layerA" in2="layerB" operator="arithmetic" k1="0" k2="1.0" k3="1.0" k4="0" result="temp"/>
<feComposite in="temp" in2="SourceGraphic" operator="arithmetic" k1="0" k2="1.0" k3="1.0" k4="0"/>
</filter>
</defs>
<g stroke="none" stroke-width="0" shape-rendering="crispEdges" >
<path d='M100,0 L200,200 L0,200 Z' fill='url(#fillR)' id='pathA'></path>
<path d='M100,0 L200,200 L0,200 Z' fill='url(#fillG)' id='pathB'></path>
<path d='M100,0 L200,200 L0,200 Z' fill='url(#fillB)'></path>
<path d="M 50,0 L 0,86 100,86 Z" fill="url(#fillB)" filter="url(#Default)" />
</g>
<!--
<path d='M100,0 L300,0 L200,200 Z' fill='#f438ff' id='triangle2'></path>
<path d='M0,200 L200,200 L100,400 Z' fill='#897457' id='triangle3'></path>
<path d='M200,200 L300,400 L100,400 Z' fill='#88aaef' id='triangle4'></path>
<path d='M300,0 L400,200 L200,200 Z' fill='#99ea21' id='triangle5'></path>
<path d='M200,200 L400,200 L300,400 Z' fill='#845879' id='triangle6'></path>
-->
</svg>
<script>
var svg = document.getElementById('svg');
</script>
<!-- <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="//d3js.org/topojson.v1.min.js"></script> -->
-->
<!--
DISCUSS:
gourad / phong?
Opacity solution lacks saturation and transition alignment
gourand / phong?
Delaunay / Voronoi
Mesh gradients / coon mesh
feImage
-->
<script>
// var file = 'uk-topo.json';
// var file = 'ocean-geo.json';
// d3.json(file, function(error, mapJson) {
// // if (error) return console.error(error);
// var projection =
// //d3.geo.albers()
// d3.geo.mercator()
// // .center([0, 55.4])
// // .rotate([4.4, 0])
// // .parallels([50, 60])
// .scale(200)
// .translate([w / 2, h / 2]);
// var path = d3.geo.path().projection(projection);
// svg.selectAll('.subunit')
// .data(mapJson.features)
// .enter().append('path')
// .attr('class', function(d) {
// console.log(d);
// return 'subunit ' + d.id;
// })
// .attr('d', path);
// // svg.selectAll('.subunit')
// // .data(topojson.feature(uk, uk.objects.subunits).features)
// // .enter().append('path')
// // .attr('class', function(d) {
// // return 'subunit ' + d.id;
// // })
// // .attr('d', path);
// // svg.append("path")
// // .datum(topojson.mesh(uk, uk.objects.subunits, function(a, b) { return a !== b && a.id !== "IRL"; }))
// // .attr("d", path)
// // .attr("class", "subunit-boundary");
// // svg.append("path")
// // .datum(topojson.mesh(uk, uk.objects.subunits, function(a, b) { return a === b && a.id === "IRL"; }))
// // .attr("d", path)
// // .attr("class", "subunit-boundary IRL");
// // svg.append("path")
// // .datum(topojson.feature(uk, uk.objects.places))
// // .attr("d", path)
// // .attr("class", "place");
// // svg.selectAll(".place-label")
// // .data(topojson.feature(uk, uk.objects.places).features)
// // .enter().append("text")
// // .attr("class", "place-label")
// // .attr("transform", function(d) { return "translate(" + projection(d.geometry.coordinates) + ")"; })
// // .attr("dy", ".35em")
// // .text(function(d) { return d.properties.name; });
// // svg.selectAll(".subunit-label")
// // .data(topojson.feature(uk, uk.objects.subunits).features)
// // .enter().append("text")
// // .attr("class", function(d) { return "subunit-label " + d.id; })
// // .attr("transform", function(d) { return "translate(" + path.centroid(d) + ")"; })
// // .attr("dy", ".35em")
// // .text(function(d) { return d.properties.name; });
// });
// var h = 800;
// var w = 800;
// var svg = d3.select("body").append("svg")
// .attr('x', 500)
// .attr("width", w)
// .attr("height", h);
</script>
</body>
</html>

Loading…
Cancel
Save