dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 2862a68801fdd48b162f3d57a3038f0562f442b8
parent ecb0159a7bef6e9bf8bab0ecca8fdfe59199b42e
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Thu, 15 Jun 2023 06:18:30 -0700

cleanup arrow code

Diffstat:
Mpkg/web/handlers/chess.go | 46++++++++++++++++++++++++++--------------------
1 file changed, 26 insertions(+), 20 deletions(-)

diff --git a/pkg/web/handlers/chess.go b/pkg/web/handlers/chess.go @@ -811,30 +811,36 @@ func calcDisc(x1, y1, x2, y2 int) (d int, isDiag, isLine bool) { return } +func arrow(s1, s2 chess.Square) (out string) { + cx1, cy1 := squareCoord(s1, false) + cx2, cy2 := squareCoord(s2, false) + dist, isDiag, isLine := calcDisc(cx1, cy1, cx2, cy2) + a := math.Atan2(float64(cy1-cy2), float64(cx1-cx2)) + math.Pi/2 + math.Pi + out += "#arrow { display: block !important; } " + out += fmt.Sprintf("#arrow { "+ + "transform: rotate(%.9frad) !important; "+ + "top: calc(%d*12.5%% + (12.5%%/2)) !important; "+ + "left: calc(%d*12.5%% + (12.5%%/2) - 6.25%%) !important; "+ + "} ", a, cy2, cx2) + var h string + if isDiag { + dist /= 2 + h = fmt.Sprintf("calc(%d*17.67%% + 10.67%%)", dist-1) + } else if isLine { + h = fmt.Sprintf("calc(%d*12.5%% + 5.5%%)", dist-1) + } else { + h = fmt.Sprintf("calc(20.95%%)") + } + out += fmt.Sprintf("#arrow .rectangle { height: %s !important; }", h) + return +} + func renderBestMove(styles *StylesBuilder, bestMove *chess.Move) { if bestMove != nil { - styles.Append("#arrow { display: block !important; }") s1 := bestMove.S1() s2 := bestMove.S2() - cx1, cy1 := squareCoord(s1, false) - cx2, cy2 := squareCoord(s2, false) - a := math.Atan2(float64(cy1-cy2), float64(cx1-cx2)) + math.Pi/2 + math.Pi - styles.Appendf("#arrow { "+ - "transform: rotate(%.9frad) !important; "+ - "top: calc(%d*12.5%% + (12.5%%/2)) !important; "+ - "left: calc(%d*12.5%% + (12.5%%/2) - 6.25%%) !important; "+ - "}", a, cy2, cx2) - dist, isDiag, isLine := calcDisc(cx1, cy1, cx2, cy2) - var h string - if isDiag { - dist /= 2 - h = fmt.Sprintf("calc(%d*17.67%% + 10.67%%)", dist-1) - } else if isLine { - h = fmt.Sprintf("calc(%d*12.5%% + 5.5%%)", dist-1) - } else { - h = fmt.Sprintf("calc(20.95%%)") - } - styles.Appendf("#arrow .rectangle { height: %s !important; }", h) + arrowStyle := arrow(s1, s2) + styles.Append(arrowStyle) } }