0442fcd544ce07eab40dff4bdb34ed2fbafc9565682d702006c95e3a4c139132ab8501dc65a54e38bd1a05c139e563815bd61324bef84bdd1da8957ad6ba36 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf8" />
  5. <title>extendNewGraph</title>
  6. <style>
  7. html, body, #canvas {
  8. width: 100%;
  9. height: 100%;
  10. margin: 0px;
  11. padding: 0px;
  12. }
  13. </style>
  14. <script src="../../dist/crender.map.js"></script>
  15. </head>
  16. <body>
  17. <canvas id="canvas"></canvas>
  18. </body>
  19. <script>
  20. const { CRender, extendNewGraph } = window.CRender
  21. const render = new CRender(document.querySelector('#canvas'))
  22. extendNewGraph('line', {
  23. shape: {
  24. x: 0,
  25. y: 0,
  26. length: 0
  27. },
  28. validator ({ shape }) {
  29. const { x, y, length } = shape
  30. if (
  31. typeof x !== 'number' ||
  32. typeof y !== 'number' ||
  33. typeof length !== 'number'
  34. ) {
  35. console.warn('line: shape attributes all must be a number!')
  36. return false
  37. }
  38. return true
  39. },
  40. draw ({ ctx }, { shape }) {
  41. const { x, y, length } = shape
  42. ctx.beginPath()
  43. ctx.moveTo(x, y)
  44. ctx.lineTo(x + length, y)
  45. ctx.closePath()
  46. ctx.stroke()
  47. }
  48. })
  49. const [w, h] = render.area
  50. const line = render.add({
  51. name: 'line',
  52. shape: {
  53. x: w / 2 - 100,
  54. y: h / 2,
  55. length: 200
  56. },
  57. style: {
  58. stroke: '#ffee97',
  59. lineWidth: 10
  60. }
  61. })
  62. </script>
  63. </html>