Skip to content

Commit

Permalink
feat(resolver-pixi): initial implementation for pixi resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
Igmat committed Mar 1, 2018
1 parent c354f44 commit dd82794
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/baset-resolver-pixi/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
!/bin/**/*
!/dist/**/*
/src/
tsconfig.json
tslint.json
22 changes: 22 additions & 0 deletions packages/baset-resolver-pixi/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2018 Ihor Chulinda

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

21 changes: 21 additions & 0 deletions packages/baset-resolver-pixi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# BaseT React resolver
> Pixi resolver for [BaseT](https://github.com/Igmat/baset) project.
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->


- [Installation and usage](#installation-and-usage)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Installation and usage
Run:
```
npm install --save-dev baset-resolver-pixi
```
and adding next line to `baset.plugins` section in your `package.json` or `plugins` section in your `.basetrc`/`.basetrc.json`:
```JSON
".spec.js$": ["baset-resolver-pixi", "baset-baseliner-json"]
```
No additional options available for this plugin for now.
45 changes: 45 additions & 0 deletions packages/baset-resolver-pixi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "baset-resolver-pixi",
"version": "0.8.0",
"description": "Pixi resolver plugin for BaseT project.",
"keywords": [
"baset-resolver-pixi",
"pixi",
"pixi.js",
"baseline",
"unit-test",
"test",
"testing",
"e2e-test"
],
"author": "Ihor Chulinda <[email protected]>",
"license": "MIT",
"repository": {
"type": "git",
"url": "[email protected]:Igmat/baset.git"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "npm run tslint && tsc",
"watch": "npm run tslint && tsc -w",
"tslint": "tslint -c tslint.json -p tsconfig.json",
"test": "baset",
"accept": "baset accept",
"doctoc": "doctoc README.md",
"prepublish": "npm run doctoc"
},
"devDependencies": {
"@types/pixi.js": "^4.7.1",
"@types/node": "^9.3.0",
"baset": "^0.8.0",
"doctoc": "^1.3.0",
"tslint": "^5.9.1",
"typescript": "next"
},
"dependencies": {
"baset-core": "^0.8.0",
"baset-vm": "^0.7.3",
"pixi.js": "^4.7.0"
}
}
22 changes: 22 additions & 0 deletions packages/baset-resolver-pixi/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { AbstractResolver, utils } from 'baset-core';
import { NodeVM } from 'baset-vm';
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';

const readFile = promisify(fs.readFile);
const renderInContextScript = readFile(path.resolve(__dirname, 'renderInContext.js'), { encoding: 'utf8' });
const matchInContextScript = readFile(path.resolve(__dirname, 'matchInContext.js'), { encoding: 'utf8'});

export default class PixiResolver extends AbstractResolver {
match = async (obj: any, context: NodeVM, sandbox: utils.IDictionary<any>) => {
sandbox.basetResolverPixi__ObjectToMatch = obj;

return context.run(await matchInContextScript, 'PixiResolver.js').matchResult;
}
resolve = async (obj: any, context: NodeVM, sandbox: utils.IDictionary<any>) => {
sandbox.basetResolverPixi__ObjectToRender = obj;

return await context.run(await renderInContextScript, 'PixiResolver.js').renderedResult;
}
}
4 changes: 4 additions & 0 deletions packages/baset-resolver-pixi/src/matchInContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'pixi.js';
declare const basetSandbox: { basetResolverPixi__ObjectToMatch: any; };

export const matchResult = basetSandbox.basetResolverPixi__ObjectToMatch instanceof PIXI.DisplayObject;
14 changes: 14 additions & 0 deletions packages/baset-resolver-pixi/src/renderInContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'pixi.js';
declare const basetSandbox: { basetResolverPixi__ObjectToRender: PIXI.DisplayObject };

const objectToRender = basetSandbox.basetResolverPixi__ObjectToRender;
const bounds = objectToRender.getBounds();
const app = new PIXI.Application(bounds.width, bounds.height);
const canvas = document.body.appendChild(app.view);

app.stage.addChild(objectToRender);
app.render();

export const renderedResult = new Promise((resolve, reject) =>
app.ticker.addOnce(() =>
resolve(canvas.toDataURL('image/png'))));
16 changes: 16 additions & 0 deletions packages/baset-resolver-pixi/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*",
"src/types/*"
]
}
},
"include": [
"src/**/*"
]
}
8 changes: 8 additions & 0 deletions packages/baset-resolver-pixi/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": [
"../../tslint.json"
],
"rules": {
"no-default-export": false
}
}

0 comments on commit dd82794

Please sign in to comment.