Git Product home page Git Product logo

amuse's Introduction

amuse

This is a Sunday night hack to see if I can create a PDF renderer using React and headless Chrome.

Try it out

npm install -g create-react-app
create-react-app --scripts-version @lostmyname/amuse-react-scripts my-personalised-book

Then follow the instructions in your console.

Aims

  • Can be run simply in cloud or development environments
  • New products can be instantly bootstrapped and deployed
  • Products can be composed without a steep learning curve

Ideas

A headless Chrome instance can be set up using Lighthouse's cross platform ChromeLauncher and PDFs can be generated programmatically using the devtools API (https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF).

I'm choosing React not just because it is a familiar tool for creating web content but also because it's an ideal paradigm for developing personalised products. The video React: rethinking best practices provides a good high level explanation of this paradigm (how reducing coupling and increasing cohesion between logic and view concerns leads to a better development experience and safer code). React also opens us up to a large ecosystem and the ability to create new building blocks whilst still using the same declarative APIs. For example, react-art is a library for drawing vector graphics and our own styleguide application could be used to document the building blocks we use.

I envision a library of React components that include basic publishing primitives such as artboards, text and images; and a higher order component that connects personalisation parameters to components.

Finally, I want this project to remove the complexity curve involved in starting work on a new product. To achieve this I'll use the extremely well maintained create-react-app. Using our own custom set of scripts, all the pain of setting up a new project and copying boilerplate can be reduced down to a single command.

This, incidentally, buys us two other benefits:

  1. this is the same approach we're taking for starting a new web project (https://github.com/lostmyname-labs/web-playbook)
  2. linting, formatting, hot reloading, transpiling are all included for free

Success

This is how I want this thing to go:

  1. Create a new product
create-react-app --scripts-version @lostmyname/amuse-react-scripts kingdom-of-you-book

This will set up build tooling, a local dev server, the ability to deploy and a boilerplate project.

  1. Hack the boilerplate

The project will include our equivalent of a Hello World app - a Once upon time book - that the developer can build upon:

// @flow
import React from 'react';
import { personalise, Artboard, Page, Text, Image } from '@lostmyname/amuse-react-components';
import frontCover from './frontCover.svg';
import backCover from './backCover.svg';

type Props = { name: string };

const Book = ({ name }): Props => (
  <Artboard
    size="A4"
    orientation="horizontal"
    bleed={5}>
    <Page>
      <Image src={frontCover} expand />
    </Page>
    <Page>
      <Text x={10} y={100}>Once upon a time...</Text>
      <Text x={40} y={100}>A child called {props.name}</Text>
      <Text x={70} y={100}>Went on an adventure</Text>
    </Page>
    <Page>
      <Text x={250} y={300}>The end.</Text>
    </Page>
    <Page>
      <Image src={backCover} expand />
    </Page>
  </Artboard>
);

export default personalise(Book);

And an example demonstrating use of more advanced primitives, and pages as individual modules:

// @flow
import React from 'react';
import { personalise, Page, Text, GraphicalText, Image, Scatter } from '@lostmyname/amuse-react-components';
import { times } from 'lodash';
import cakeSrc from './cake.svg';
import candleBoundarySrc from './candleBoundary.svg';

type Props = { name: string, age: number };

const IntroductionPage = ({ name, age }) : Props => (
  <Page>
    <Text x={100} y={200}>One morning {name} woke up....</Text>
    <Image src={cakeSrc}>
      <Scatter within={candleBoundarySrc}>
        {times(age, i => (
          <Image data={candle} index={i}>
        ))}
      </Scatter>
      <GraphicalText texture="icing">Happy Birthday {name}</GraphicalText>
    </Image>
  </Page>
);

export default personalise(IntroductionPage);
  1. Render to browser
yarn run start

Will launch a web browser as a render target with hot reloading capabilities.

Navigating to http://localhost:4000 will render every page in the artboard. A single page can be rendered by navigating to http://localhost:4000/page/1.

  1. Render to PDF

Adding the query parameter ?pdf=true will make the server respond with a PDF file instead of a HTML page.

  1. Deploy

The generated app will include a Procfile and app.json so developers can easily deploy their product to heroku and setup a deployment pipeline.

amuse's People

Contributors

gaearon avatar fson avatar timer avatar vjeux avatar djgrant avatar mxstbr avatar existentialism avatar lacker avatar johann-sonntagbauer avatar n3tr avatar enoahnetzach avatar viankakrisna avatar dceddia avatar hnordt avatar keyz avatar eanplatter avatar tuchk4 avatar pd4d10 avatar valscion avatar bebbi avatar nhajidin avatar mareksuscak avatar kripod avatar koistya avatar insin avatar frontsideair avatar eliperelman avatar alexzherdev avatar sheerun avatar manavsehgal avatar

Watchers

Tal Oron avatar Robert Watts avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.