Skip to content

Instantly share code, notes, and snippets.

@jtenner
Last active June 12, 2019 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtenner/00765a7e2ebe01888384e2924cfba93b to your computer and use it in GitHub Desktop.
Save jtenner/00765a7e2ebe01888384e2924cfba93b to your computer and use it in GitHub Desktop.
import { SetTransformOperation } from "../internal/operations/SetTransformOperation";
import { CanvasContext2DInstructionType } from "../../src/shared/CanvasContext2DInstructionType";
describe("SetTransformOperation", () => {
it("should write to a buffer", () => {
// Create an array buffer with the size of a SetTransformOperation
let buffer = new ArrayBuffer(offsetof<SetTransformOperation>());
// write to the buffer at index 0
let nextIndex = SetTransformOperation.write(buffer, 0, 1, 2, 3, 4, 5, 6);
let comparison = new SetTransformOperation(
<f64>CanvasContext2DInstructionType.SetTransform,
8.0,
1.0,
2.0,
3.0,
4.0,
5.0,
6.0,
);
expect<SetTransformOperation>(changetype<SetTransformOperation>(buffer))
.toStrictEqual(comparison, "The SetTransformOperation values should be the same.");
expect<i32>(nextIndex)
.toBe(8, "The next index should be 8, because it wrote 8 floats to the buffer.");
});
});
import { CanvasContext2DInstructionType } from "../../../src/shared/CanvasContext2DInstructionType";
export class SetTransformOperation {
public static OPERATION_OFFSET: i32 = 8;
public constructor(
public instruction: f64,
public offset: f64,
public a: f64,
public b: f64,
public c: f64,
public d: f64,
public e: f64,
public f: f64,
) {}
/**
* This method writes a SetTransformOperation to a buffer.
*
* @param {ArrayBuffer} buffer - The target buffer to write the instruction to.
* @param {i32} bufferIndex - The index of the buffer to write to.
* @param {f64} a - The a value of the matrix.
* @param {f64} b - The b value of the matrix.
* @param {f64} c - The c value of the matrix.
* @param {f64} d - The d value of the matrix.
* @param {f64} e - The e value of the matrix.
* @param {f64} f - The f value of the matrix.
* @returns {i32} - The next bufferIndex location.
*/
@inline
public static write(buffer: ArrayBuffer, bufferIndex: i32, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64): i32 {
/** Assert that there are enough free bytes to write to. */
assert(
(buffer.byteLength >> alignof<f64>()) <= (bufferIndex + SetTransformOperation.OPERATION_OFFSET),
"BufferOverflow: "
);
/** Get the pointer to the location where the buffer will be written to. */
let index = bufferIndex + changetype<usize>(buffer);
/** Store each instruction value at the specified offsets located within this class. */
store<f64>(index, CanvasContext2DInstructionType.SetTransform, offsetof<SetTransformOperation>("instruction"));
store<f64>(index, <f64>SetTransformOperation.OPERATION_OFFSET, offsetof<SetTransformOperation>("offset"));
store<f64>(index, a, offsetof<SetTransformOperation>("a"));
store<f64>(index, b, offsetof<SetTransformOperation>("b"));
store<f64>(index, c, offsetof<SetTransformOperation>("c"));
store<f64>(index, d, offsetof<SetTransformOperation>("d"));
store<f64>(index, e, offsetof<SetTransformOperation>("e"));
store<f64>(index, f, offsetof<SetTransformOperation>("f"));
/** Return the next writable index. */
return bufferIndex + SetTransformOperation.OPERATION_OFFSET;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment