HTML Canvas Unit testing

前端 未结 6 860
既然无缘
既然无缘 2020-12-08 13:46

How can I unit-test Javascript that draws on an HTML canvas? Drawing on the canvas should be checked.

6条回答
  •  没有蜡笔的小新
    2020-12-08 14:11

    Since the "shapes" and "lines" drawn on a canvas are not actual objects (it's like ink on paper), it would be very hard (impossible?) to do a normal unit test on that.

    The best you can do with standard canvas it analyze the pixel data (from the putImageData/getImageData. Like what bedraw was saying).

    Now, I haven't tried this yet, but it might be more what you need. Cake is a library for the canvas. It's using alot of the putImageData/getImageData. This example might help with what you are trying to do with a test.

    Hope that helps answer your question.

提交回复
热议问题