[Fix] Errors when updating max hp and implement bar update animations (#1883)

* Fix an error that can break the canvas when importing or resetting a character

* Animate bar updates

* Use non-static mix function instead
This commit is contained in:
Carlos Fernandez 2026-05-15 03:21:05 -04:00 committed by GitHub
parent 6b4de71a0a
commit 855f4549ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -249,9 +249,6 @@ export default class DhTokenPlaceable extends foundry.canvas.placeables.Token {
/** @inheritDoc */ /** @inheritDoc */
_drawBar(number, bar, data) { _drawBar(number, bar, data) {
const val = Number(data.value);
const pct = Math.clamp(val, 0, data.max) / data.max;
// Determine sizing // Determine sizing
const { width, height } = this.document.getSize(); const { width, height } = this.document.getSize();
const s = canvas.dimensions.uiScale; const s = canvas.dimensions.uiScale;
@ -259,17 +256,19 @@ export default class DhTokenPlaceable extends foundry.canvas.placeables.Token {
const bh = 8 * (this.document.height >= 2 ? 1.5 : 1) * s; const bh = 8 * (this.document.height >= 2 ? 1.5 : 1) * s;
// Determine the color to use // Determine the color to use
const fillColor = const Color = foundry.utils.Color;
number === 0 ? foundry.utils.Color.fromRGB([1, 0, 0]) : foundry.utils.Color.fromString('#0032b1'); const fillColor = number === 0 ? Color.fromRGB([1, 0, 0]) : Color.fromString('#0032b1');
const emptyColor = Color.fromRGB([0, 0, 0]);
// Draw the bar // Draw the bar (accounting floating point numbers from bar animations)
const widthUnit = bw / data.max; const widthUnit = bw / Math.ceil(data.max);
bar.clear().lineStyle(s, 0x000000, 1.0); bar.clear().lineStyle(s, 0x000000, 1.0);
const sections = [...Array(data.max).keys()]; const sections = [...Array(Math.ceil(data.max)).keys()];
for (let mark of sections) { for (const mark of sections) {
const x = mark * widthUnit; const x = mark * widthUnit;
const marked = mark + 1 <= data.value; const marked = mark < Math.ceil(data.value);
const color = marked ? fillColor : foundry.utils.Color.fromRGB([0, 0, 0]); const remainder = mark === Math.ceil(data.value) - 1 ? data.value % 1 : 0;
const color = !marked ? emptyColor : remainder ? emptyColor.mix(fillColor, remainder) : fillColor;
if (mark === 0 || mark === sections.length - 1) { if (mark === 0 || mark === sections.length - 1) {
bar.beginFill(color, marked ? 1.0 : 0.5).drawRect(x, 0, widthUnit, bh, 2 * s); // Would like drawRoundedRect, but it's very troublsome with the corners. Leaving for now. bar.beginFill(color, marked ? 1.0 : 0.5).drawRect(x, 0, widthUnit, bh, 2 * s); // Would like drawRoundedRect, but it's very troublsome with the corners. Leaving for now.
} else { } else {