drawingChooser = function(drawings, w) {
let drawingLine = d3.line()
.x(function(d) { return d.x })
.y(function(d) { return d.y })
.curve(d3.curveBasis)
var size = 40
var cols = Math.floor(w/size) - 1;
var filtered = drawings.slice(0, cols * cols)
var scale = size/300
var h = filtered.length/cols * size + size * 2
var el = DOM.svg(w, h);
var svg = d3.select(el)
var g = svg.append("g").attr("transform", "translate(0, 0)")
var positioned = filtered.map(function(d,i) {
var x = size/2 + (i % cols) * size
var y = size/2 + Math.floor(i/cols) * size
var strokes = center(d.strokes,size,size,scale * 1.15)
strokes.forEach(function(s) {
s.nstrokes = strokes.length
s.recognized = d.recognized
})
return {
x: x,
y: y,
i: i,
data: d,
strokes: strokes
}
})
var strokecolor = function(t) { return d3.interpolatePurples((1 - t) * 0.6 + 0.4) }
var unreccolor = function(t) { return d3.interpolateReds((1 - t) * 0.6 + 0.4) }
var bgs = g
.selectAll("g.drawing")
.data(positioned)
.enter().append("g").classed("drawing", true)
bgs.attr("transform", function (d, i) {
return "translate(" + [d.x, d.y] + ")";
})
.style("stroke-width", function (d) {
return 0.75;
});
bgs.append("rect").attr("width", size).attr("height", size).style("fill", "#fff")
.style("fill-opacity", 0)
.style("stroke", "none")
bgs.selectAll("path.stroke").data(function(d) {
return d.strokes
})
.enter().append("path").classed("stroke", true)
.attr("d", drawingLine)
.style("fill", "none")
.style("stroke-linecap", "round")
.style("pointer-events", "all")
.style("stroke", function(d,i) {
if(d.recognized)
return strokecolor(i/d.nstrokes)
return unreccolor(i/d.nstrokes)
})
bgs
.on("click", function(evt, d) {
choose(d.data)
})
.on("mouseover", function(evt, d) {
d3.select(this).style("stroke-width", 1.5)
})
.on("mouseout", function(evt, d) {
d3.select(this).style("stroke-width", 0.75)
})
function choose(d) {
console.log("chose", d)
el.value = d
el.dispatchEvent(new CustomEvent("input"))
el.dispatchEvent(new CustomEvent("input"))
g.selectAll("g.drawing").selectAll("path.stroke")
.style("stroke", function(d,i) {
if(d.recognized)
return strokecolor(i/d.nstrokes)
return unreccolor(i/d.nstrokes)
})
g.selectAll("g.drawing")
.style("stroke-width", 0.75)
.filter(function(f) { return f.data == d })
.selectAll("path.stroke")
.style("stroke", "#26a69a")
.style("stroke-width", 2)
}
choose(drawings[0])
return el
}