hw 2 done

This commit is contained in:
=
2026-03-13 17:50:03 +10:00
parent ed21fb4694
commit 757d60e2d3
4 changed files with 50 additions and 33 deletions

View File

@@ -3,31 +3,31 @@
function drawSmile(svg) {
let smile = svg.append("g")
.style("stroke", "brown")
.style("stroke-width", 2)
.style("stroke-width", 0)
.style("fill", "brown");
//лицо
smile.append("circle")
.attr("cx", 0)
.attr("cy", 0)
.attr("r", 50)
.style("fill", "yellow");
//левый глаз
smile.append("circle")
.attr("cx", -20)
.attr("cy", -10)
.attr("r", 5);
//правый глаз
smile.append("circle")
.attr("cx", 20)
.attr("cy", -10)
.attr("r", 5);
// улыбка
let arc = d3.arc()
.innerRadius(35)
.outerRadius(35);
smile.append("path")
.attr("d", arc({startAngle: Math.PI /3 * 2, endAngle: Math.PI/3 * 4}))
.style("stroke", "brown")
return smile
for (let t = 0; t <= Math.PI * 6; t += .8) {
let cords = {
x: 50 / 3 * (1 - (t / (Math.PI * 6))) * -Math.sin(t),
y: 50 / 3 * (1 - (t / (Math.PI * 6))) * Math.cos(t)
}
smile.append("circle")
.attr("cx", cords["x"])
.attr("cy", cords["y"])
.attr("r", (1 - (t / (Math.PI * 6))))
.style("fill", "red");
}
for (let t = .4; t <= Math.PI * 6; t += .8) {
let cords = {
x: 50 / 3 * (1 - (t / (Math.PI * 6))) * -Math.sin(t),
y: 50 / 3 * (1 - (t / (Math.PI * 6))) *Math.cos(t)
}
smile.append("circle")
.attr("cx", cords["x"])
.attr("cy", cords["y"])
.attr("r", (1 - (t / (Math.PI * 6))))
.style("fill", "green");
}
return smile
}

View File

@@ -57,10 +57,15 @@ const animRouter = (dataForm) => {
let path = drawPath(Number(dataForm.pathSelect.value));
const svg = d3.select("svg")
let pict = drawSmile(svg);
const parameters = {
sx:[Number(dataForm.sx.value),Number(dataForm.sx_finish.value)],
sy:[Number(dataForm.sy.value),Number(dataForm.sy_finish.value)],
r:[Number(dataForm.r.value),Number(dataForm.r_finish.value)],
};
pict.transition()
.ease([d3.easeLinear, d3.easeElastic, d3.easeBounce][Number(animTypeSelect.value)])
.duration(6000)
.attrTween('transform', translateAlong(path.node()));
.duration(Number(duration.value))
.attrTween('transform', translateAlong(path.node(),parameters));
}
else {
runAnimation(dataForm)
@@ -77,7 +82,7 @@ const runAnimation = (dataForm) => {
${dataForm.sy.value})
rotate(${dataForm.r.value})`)
.transition()
.duration(6000)
.duration(Number(duration.value))
.ease([d3.easeLinear, d3.easeElastic, d3.easeBounce][Number(animTypeSelect.value)])
.attr("transform", `
translate(${dataForm.cx_finish.value},

View File

@@ -82,12 +82,18 @@ const drawPath = (typePath) => {
return path;
}
function translateAlong(path) {
function translateAlong(path, params) {
const length = path.getTotalLength();
return function () {
return function (t) {
const { x, y } = path.getPointAtLength(t * length);
return `translate(${x},${y})`;
return `
translate(${x},${y})
scale(${params.sx[0] +(params.sx[1] - params.sx[0])*t},
${params.sy[0] +(params.sy[1] - params.sy[0])*t})
rotate(${params.r[0] +(params.r[1] - params.r[0])*t})
`;
}
}
}