<html> <!DOCTYPE html> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.js"></script> <div id="input_group"> <div id = "maxhp"> <div>血量上限:<a id = "maxhp_text">22000</a></div> <input id = "maxhp_slider" type="range" min="12000" max="25000" value="22000" onchange="onChange()"> </div> <div id = "bhp"> <div>初始血量:<a id = "bhp_text">22000</a></div> <input id = "bhp_slider" type="range" min="1" max="25000" value="22000" onchange="onChange()"> </div> <div id = "damage"> <div>对手伤害:<a id = "damage_text">11000</a></div> <input id = "damage_slider" type="range" min="1000" max="25000" value="11000" onchange="onChange()"> </div> <div id = "ro"> <div>预览回合:<a id = "ro_text">5</a></div> <input id = "ro_slider" type="range" min="1" max="15" value="5" onchange="onChange()"> </div> </div> <div id="bar_hp"></div> <script> var t_r = document.getElementById("ro_text"); var t_m = document.getElementById("maxhp_text"); var t_b = document.getElementById("bhp_text"); var t_d = document.getElementById("damage_text"); var round = +document.getElementById("ro_slider").value; var max_hp = +document.getElementById("maxhp_slider").value; var damage0 = +document.getElementById("damage_slider").value; if (damage0 > max_hp) { damage0 = max_hp; } var damage = damage0/max_hp; var begin_hp = +document.getElementById("bhp_slider").value; t_r.innerText = round; t_m.innerText = max_hp; t_b.innerText = begin_hp; t_d.innerText = damage0;
function onChange(){ round = +document.getElementById("ro_slider").value; max_hp = +document.getElementById("maxhp_slider").value; damage0 = +document.getElementById("damage_slider").value; if (damage0 > max_hp) { damage0 = max_hp; document.getElementById("damage_slider").value = max_hp; } damage = damage0/max_hp; begin_hp = +document.getElementById("bhp_slider").value; t_r.innerText = round; t_m.innerText = max_hp; t_b.innerText = begin_hp; t_d.innerText = damage0; d3.select("svg").remove(); applyAll(); }
var margin = {top: 20, right: 30, bottom: 40, left: 90}, width = 460 - margin.left - margin.right, height = 400 - margin.top - margin.bottom;
function applyAll() { function current_hp(n, y) { var cur_hp = [begin_hp/max_hp]; for (var i = 1; i<=n;i++) { if (cur_hp[i-1] >= 0.8) { cur_hp.push(cur_hp[i-1] - y); } else { cur_hp.push((1+y) * cur_hp[i-1] - 0.8 * y); } } return cur_hp; }
function survive_y(n) { var l, r, y; l = 0.2, r = 1; var pre = 0.01; while (r - l > pre) { y = (l+r)/2; if (current_hp(n, y)[n] > 0) { l = y; } else { r = y; } } return y; }
var data1 = []; for (var i = 1; i <= round; i++){ var temp = {"round": i, "hp": survive_y(i)}; data1.push(temp); }
var data2 = []; for (var i = 1; i < 10000000; i++) { var res = current_hp(i, damage)[i]; if (res >= 0) { data2.push({"round": i, "hp": res}); } else { data2.push({"round": i, "hp": 0}); break; } }
var svg = d3.select("#bar_hp") .append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var x = d3.scaleLinear() .domain([0, max_hp]) .range([ 0, width]); svg.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)) .selectAll("text") .attr("transform", "translate(-10,0)rotate(-45)") .style("text-anchor", "end");
var y = d3.scaleBand() .range([ 0, height]) .domain(data1.map(function(d) { return d.round; })) .padding(.1); svg.append("g") .call(d3.axisLeft(y))
function update() { var u = svg.selectAll("myRect") .data(data1); u.enter() .append("rect") .attr("x", x(0) ) .attr("y", function(d) { return y(d.round); }) .attr("width", function(d) { return x(d.hp)*max_hp; }) .attr("height", y.bandwidth() ) .attr("fill", "#69b3a2") } update(); }
applyAll();
</script> </html>
|