commit d3374edc3d556b2d4eb8ecd9b146255fa5e5686c
parent 088f583d2b45f329a8d0ee148f0e642f4734c997
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Thu, 15 Jun 2023 06:00:42 -0700
perfect arrows
Diffstat:
2 files changed, 39 insertions(+), 18 deletions(-)
diff --git a/pkg/web/handlers/chess.go b/pkg/web/handlers/chess.go
@@ -795,26 +795,46 @@ func renderHideAllPieces(styles *StylesBuilder) {
// return math.Sqrt(math.Pow(p2.GetX()-p1.GetX(), 2) + math.Pow(p2.GetY()-p1.GetY(), 2))
//}
+func calcDisc(x1, y1, x2, y2 int) (d int, isDiag, isLine bool) {
+ dx := int(math.Abs(float64(x2 - x1)))
+ dy := int(math.Abs(float64(y2 - y1)))
+ if x1 == x2 {
+ d = dy
+ isLine = true
+ } else if y1 == y2 {
+ d = dx
+ isLine = true
+ } else {
+ d = dx + dy
+ }
+ isDiag = dx == dy
+ 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-origin: top center !important; "+
- // "transform: rotate(%.9frad) !important; "+
- // "top: calc(%d*12.5%% + (12.5%%/2)) !important; "+
- // "left: calc(%d*12.5%% + (12.5%%/2) - 25px) !important; "+
- // "}", a, cy2, cx2)
- //// 12.5%
- ////
- //styles.Append("#arrow .rectangle { height: calc(4*5.5%) !important; }")
-
- styles.Appendf(`.square_%d, .square_%d { background-color: rgba(0, 0, 255, 0.2) !important; }`,
- int(bestMove.S1()), int(bestMove.S2()))
+ 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)
}
}
diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go
@@ -136,6 +136,7 @@ func GetID(row, col int, isFlipped bool) (id int) {
var ChessCSS = `
<style>
#arrow {
+ transform-origin: top center !important;
display: none;
position: absolute;
top: 0;