NinePatch

We often encounter some pictures that are not fixed in size, but the surrounding or four-pass style is not deformed, that is, the .9 picture, such as a message bubble. If you directly set the width and height, the entire bubble picture will be deformed. In the picture below, the second one is a bubble created by Jiugongge, and the last one is created by directly stretching the picture. The first two are clearly in line with expectations. Demo

image.png

Install

With NPM

  1. npm i @eva/plugin-renderer @eva/plugin-renderer-nine-patch

In Browser

  1. <script src="https://unpkg.com/@eva/plugin-renderer-nine-patch@1.2.x/dist/EVA.plugin.renderer.ninePatch.min.js"></script>

Usage

  1. import {Game, GameObject, resource, RESOURCE_TYPE} from '@eva/eva.js'
  2. import {RendererSystem} from '@eva/plugin-renderer'
  3. import {NinePatch, NinePatchSystem} from '@eva/plugin-renderer-nine-patch'
  4. resource.addResource([
  5. {
  6. name:'nine',
  7. type: RESOURCE_TYPE.IMAGE,
  8. src: {
  9. image: {
  10. type:'png',
  11. url:'https://img.alicdn.com/tfs/TB17uSKkQ9l0K4jSZFKXXXFjpXa-363-144.png'
  12. }
  13. },
  14. preload: false
  15. }
  16. ])
  17. const game = new Game({
  18. systems: [
  19. new RendererSystem({
  20. canvas: document.querySelector('#canvas'),
  21. width: 750,
  22. height: 1000,
  23. backgroundColor: 0xffffff
  24. }),
  25. new NinePatchSystem()
  26. ]
  27. })
  28. const patch = new GameObject('patch', {
  29. size: {width: 360, height: 145 },
  30. origin: {x: 0, y: 0 },
  31. position: {
  32. x: 10,
  33. y: 10
  34. },
  35. anchor: {
  36. x: 0,
  37. y: 0
  38. }
  39. })
  40. const ninePatch = patch.addComponent(
  41. new NinePatch({
  42. resource:'nine',
  43. leftWidth: 100,
  44. topHeight: 40,
  45. rightWidth: 40,
  46. bottomHeight: 40
  47. })
  48. )
  49. const patch1 = new GameObject('patch1', {
  50. size: {width: 660, height: 345 },
  51. origin: {x: 0, y: 0 },
  52. position: {
  53. x: 10,
  54. y: 300
  55. },
  56. anchor: {
  57. x: 0,
  58. y: 0
  59. }
  60. })
  61. const ninePatch1 = patch1.addComponent(
  62. new NinePatch({
  63. resource:'nine',
  64. leftWidth: 100,
  65. topHeight: 40,
  66. rightWidth: 40,
  67. bottomHeight: 40
  68. })
  69. )
  70. game.scene.addChild(patch)
  71. game.scene.addChild(patch1)

Options

resource string

Resource Name

spriteName

This property can be set if the resource type is Sprite

leftWidth

Correspond to Figure A below

topHeight

Correspond to Figure C below

rightWidth

Correspond to Figure B below

bottomHeight

Corresponding to the figure D below

image.png