|
| 1 | +/** |
| 2 | + * canvas-renderer |
| 3 | + * https://github.com/dmester/canvas-renderer |
| 4 | + * |
| 5 | + * Copyright (c) 2017-2018 Daniel Mester Pirttijärvi |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 8 | + * a copy of this software and associated documentation files (the |
| 9 | + * "Software"), to deal in the Software without restriction, including |
| 10 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 11 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | + * permit persons to whom the Software is furnished to do so, subject to |
| 13 | + * the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be |
| 16 | + * included in all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 21 | + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 22 | + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 23 | + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 24 | + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +"use strict"; |
| 28 | + |
| 29 | + |
| 30 | +/** |
| 31 | + * canvas-renderer will use a palette when possible. To force it to produce |
| 32 | + * a truecolor PNG we need to ensure the image contains more than 256 colors. |
| 33 | + * This is done by rendering large circles, leading to a lot of different |
| 34 | + * colors. |
| 35 | + * |
| 36 | + * A mix of light and dark colors are used to ensure we test colors that |
| 37 | + * are represented as integers where the sign bit is set and unset. |
| 38 | + */ |
| 39 | + |
| 40 | +const fs = require("fs"); |
| 41 | +const tap = require("tap"); |
| 42 | +const canvasRenderer = require("../index"); |
| 43 | + |
| 44 | +var canvas = canvasRenderer.createCanvas(1000, 1000); |
| 45 | +canvas.backColor = "#abcdefff"; |
| 46 | + |
| 47 | +var ctx = canvas.getContext("2d"); |
| 48 | + |
| 49 | +var radius = 250; |
| 50 | + |
| 51 | +ctx.beginPath(); |
| 52 | +ctx.fillStyle = "#ffffffff"; |
| 53 | +ctx.moveTo(100 + radius, 250 + radius); |
| 54 | +ctx.arc(100 + radius, 250 + radius, radius, 0, Math.PI * 2, true); // last argument should have no effect |
| 55 | +ctx.closePath(); |
| 56 | +ctx.fill(); |
| 57 | + |
| 58 | +ctx.beginPath(); |
| 59 | +ctx.fillStyle = "#000000ff"; |
| 60 | +ctx.moveTo(400 + radius, 250 + radius); |
| 61 | +ctx.arc(400 + radius, 250 + radius, radius, 0, Math.PI * 2, false); // last argument should have no effect |
| 62 | +ctx.closePath(); |
| 63 | +ctx.fill(); |
| 64 | + |
| 65 | +var expected = fs.readFileSync(__dirname + "/expected-truecolor.png"); |
| 66 | +var expectedB64 = Buffer.from(expected).toString("base64"); |
| 67 | +tap.equal("data:image/png;base64," + expectedB64, canvas.toDataURL()); |
0 commit comments