123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- var tape = require("tape"),
- Canvas = require("canvas"),
- d3 = require("d3"),
- path = require("path"),
- fs = require("fs"),
- initializeCanvas = require("../audiogram/initialize-canvas.js"),
- drawFrames = require("../audiogram/draw-frames.js");
-
- const { Image } = require("canvas");
-
- require("mkdirp").sync(path.join(__dirname, "tmp", "frames"));
-
- var frameDir = path.join(__dirname, "tmp", "frames");
-
- var waveform = [
- [
- [0, 0], [1, 1], [0, 0]
- ],
- [
- [1, 1], [0.1, 0.1], [1, 1]
- ]
- ];
-
- function tester(options) {
-
- return function(test) {
-
- initializeCanvas(options, function(err, renderer){
-
- test.error(err);
-
- drawFrames(renderer, {
- numFrames: waveform.length,
- frameDir: frameDir,
- width: options.width,
- height: options.height,
- waveform: waveform
- }, function(err){
- test.error(err);
- checkFrame(test, options);
- });
-
- });
-
- };
-
- }
-
- tape.test("Draw frame", tester({
- width: 1280,
- height: 720,
- backgroundColor: "#f00",
- foregroundColor: "#fff",
- waveTop: 340,
- waveBottom: 380
- }));
-
- tape.test("Default colors", tester({
- width: 1280,
- height: 720,
- waveTop: 340,
- waveBottom: 380
- }));
-
- tape.test("Square frame", tester({
- width: 720,
- height: 720,
- backgroundColor: "#f00",
- foregroundColor: "#fff",
- waveTop: 340,
- waveBottom: 380
- }));
-
- function checkFrame(test, options) {
-
- var testCanvas = Canvas.createCanvas(options.width, options.height),
- context = testCanvas.getContext("2d");
-
- d3.queue()
- .defer(fs.readFile, path.join(frameDir, "000001.png"))
- .defer(fs.readFile, path.join(frameDir, "000002.png"))
- .await(function(e, f1, f2){
-
- test.error(e);
-
- const img = new Image();
- img.src = f1;
-
- var bg = getColor(options.backgroundColor || "#fff"),
- fg = getColor(options.waveColor || options.foregroundColor || "#000");
-
- context.drawImage(img, 0, 0, options.width, options.height);
- test.deepEqual(getColor(context.getImageData(0, 0, 1, 1)), bg);
- test.deepEqual(getColor(context.getImageData(options.width - 1, options.height - 1, 1, 1)), bg);
- test.deepEqual(getColor(context.getImageData(0, options.height / 2 - 10, 1, 1)), bg);
- test.deepEqual(getColor(context.getImageData(options.width - 1, options.height / 2 + 10, 1, 1)), bg);
- test.deepEqual(getColor(context.getImageData(options.width / 2, options.height / 2, 1, 1)), fg);
- test.deepEqual(getColor(context.getImageData(options.width / 2, options.height / 2 - 10, 1, 1)), fg);
- test.deepEqual(getColor(context.getImageData(options.width / 2, options.height / 2 + 10, 1, 1)), fg);
-
- img.src = f2;
-
- context.drawImage(img, 10, 0, options.width, options.height);
- test.deepEqual(getColor(context.getImageData(options.width / 2, options.height / 2, 1, 1)), fg);
- test.deepEqual(getColor(context.getImageData(options.width / 2 - 10, options.height / 2 - 10, 1, 1)), bg);
- test.deepEqual(getColor(context.getImageData(options.width / 2 - 10, options.height / 2 + 10, 1, 1)), bg);
-
-
- test.end();
-
- });
-
- }
-
- function getColor(c) {
- if (typeof c === "string") {
- c = d3.color(c);
- return [c.r, c.g, c.b];
- } else {
- return Array.prototype.slice.call(c.data, 0, 3);
- }
- }
-
- // Cleanup
- tape.onFinish(function(){
- require("rimraf")(path.join(__dirname, "tmp"), function(err){
- if (err) {
- throw err;
- }
- });
- });
|