Skip to content

Commit

Permalink
Support variable export sizes (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
TETRA2000 authored and jakedex committed Feb 25, 2018
1 parent b1ef2d1 commit b7adab9
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 7 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,15 @@
"contributions": [
"code"
]
},
{
"login": "TETRA2000",
"name": "Takahiko Inayama",
"avatar_url": "https://avatars1.githubusercontent.com/u/1459450?v=4",
"profile": "https://www.behance.net/tetra2000",
"contributions": [
"code"
]
}
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ Thanks goes out to all these wonderful people ([emoji key](https://github.com/ke
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars1.githubusercontent.com/u/225946?v=4" width="100px;"/><br /><sub><b>otobrglez</b></sub>](https://github.com/otobrglez)<br />[💻](https://github.com/dawnlabs/carbon/commits?author=otobrglez "Code") | [<img src="https://avatars3.githubusercontent.com/u/11488612?v=4" width="100px;"/><br /><sub><b>darahak</b></sub>](https://github.com/darahak)<br />[📖](https://github.com/dawnlabs/carbon/commits?author=darahak "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/246651?v=4" width="100px;"/><br /><sub><b>dom96</b></sub>](https://github.com/dom96)<br />[💻](https://github.com/dawnlabs/carbon/commits?author=dom96 "Code") | [<img src="https://avatars3.githubusercontent.com/u/784056?v=4" width="100px;"/><br /><sub><b>elrumordelaluz</b></sub>](https://github.com/elrumordelaluz)<br />[💻](https://github.com/dawnlabs/carbon/commits?author=elrumordelaluz "Code") | [<img src="https://avatars2.githubusercontent.com/u/21217?v=4" width="100px;"/><br /><sub><b>cjb</b></sub>](https://github.com/cjb)<br />[💻](https://github.com/dawnlabs/carbon/commits?author=cjb "Code") | [<img src="https://avatars1.githubusercontent.com/u/5427083?v=4" width="100px;"/><br /><sub><b>Krzysztof-Cieslak</b></sub>](https://github.com/Krzysztof-Cieslak)<br />[💻](https://github.com/dawnlabs/carbon/commits?author=Krzysztof-Cieslak "Code") | [<img src="https://avatars0.githubusercontent.com/u/2369851?v=4" width="100px;"/><br /><sub><b>fernahh</b></sub>](https://github.com/fernahh)<br />[📖](https://github.com/dawnlabs/carbon/commits?author=fernahh "Documentation") |
| [<img src="https://avatars1.githubusercontent.com/u/10941033?v=4" width="100px;"/><br /><sub><b>g3r4n</b></sub>](https://github.com/g3r4n)<br />[💻](https://github.com/dawnlabs/carbon/commits?author=g3r4n "Code") | [<img src="https://avatars0.githubusercontent.com/u/55830?v=4" width="100px;"/><br /><sub><b>Mat Gadd</b></sub>](http://drarok.com/)<br />[🐛](https://github.com/dawnlabs/carbon/issues?q=author%3ADrarok "Bug reports") [💻](https://github.com/dawnlabs/carbon/commits?author=Drarok "Code") | [<img src="https://avatars1.githubusercontent.com/u/11805775?v=4" width="100px;"/><br /><sub><b>Brad Davies</b></sub>](https://bradlab.co.uk)<br />[🐛](https://github.com/dawnlabs/carbon/issues?q=author%3Avarbrad "Bug reports") [💻](https://github.com/dawnlabs/carbon/commits?author=varbrad "Code") | [<img src="https://avatars1.githubusercontent.com/u/9087886?v=4" width="100px;"/><br /><sub><b>Rafael Câmara</b></sub>](http://www.rafaelcamaram.com/)<br />[💻](https://github.com/dawnlabs/carbon/commits?author=rafaelcamaram "Code") | [<img src="https://avatars1.githubusercontent.com/u/2212006?v=4" width="100px;"/><br /><sub><b>Gleb Bahmutov</b></sub>](https://glebbahmutov.com/)<br />[⚠️](https://github.com/dawnlabs/carbon/commits?author=bahmutov "Tests") [🔧](#tool-bahmutov "Tools") | [<img src="https://avatars2.githubusercontent.com/u/10677789?v=4" width="100px;"/><br /><sub><b>Iván Munguía</b></sub>](https://ivan-munguia.netlify.com)<br />[💻](https://github.com/dawnlabs/carbon/commits?author=warborn "Code") | [<img src="https://avatars1.githubusercontent.com/u/2755722?v=4" width="100px;"/><br /><sub><b>Dillon Mulroy</b></sub>](https://dillonmulroy.com)<br />[💻](https://github.com/dawnlabs/carbon/commits?author=dmmulroy "Code") |
| [<img src="https://avatars2.githubusercontent.com/u/253398?v=4" width="100px;"/><br /><sub><b>Nihad Abbasov</b></sub>](https://github.com/NARKOZ)<br />[💻](https://github.com/dawnlabs/carbon/commits?author=NARKOZ "Code") | [<img src="https://avatars1.githubusercontent.com/u/7328625?v=4" width="100px;"/><br /><sub><b>Hugo Torzuoli</b></sub>](https://torzuolih.github.io)<br />[📖](https://github.com/dawnlabs/carbon/commits?author=TorzuoliH "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/1906977?v=4" width="100px;"/><br /><sub><b>Bruno C. Couto</b></sub>](https://github.com/bruno02221)<br />[💻](https://github.com/dawnlabs/carbon/commits?author=bruno02221 "Code") | [<img src="https://avatars2.githubusercontent.com/u/13263073?v=4" width="100px;"/><br /><sub><b>Mark Molnar</b></sub>](https://github.com/molnarmark)<br />[💻](https://github.com/dawnlabs/carbon/commits?author=molnarmark "Code") |
| [<img src="https://avatars2.githubusercontent.com/u/253398?v=4" width="100px;"/><br /><sub><b>Nihad Abbasov</b></sub>](https://github.com/NARKOZ)<br />[💻](https://github.com/dawnlabs/carbon/commits?author=NARKOZ "Code") | [<img src="https://avatars1.githubusercontent.com/u/7328625?v=4" width="100px;"/><br /><sub><b>Hugo Torzuoli</b></sub>](https://torzuolih.github.io)<br />[📖](https://github.com/dawnlabs/carbon/commits?author=TorzuoliH "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/1906977?v=4" width="100px;"/><br /><sub><b>Bruno C. Couto</b></sub>](https://github.com/bruno02221)<br />[💻](https://github.com/dawnlabs/carbon/commits?author=bruno02221 "Code") | [<img src="https://avatars2.githubusercontent.com/u/13263073?v=4" width="100px;"/><br /><sub><b>Mark Molnar</b></sub>](https://github.com/molnarmark)<br />[💻](https://github.com/dawnlabs/carbon/commits?author=molnarmark "Code") | [<img src="https://avatars1.githubusercontent.com/u/1459450?v=4" width="100px;"/><br /><sub><b>Takahiko Inayama</b></sub>](https://www.behance.net/tetra2000)<br />[💻](https://github.com/dawnlabs/carbon/commits?author=TETRA2000 "Code") |
<!-- ALL-CONTRIBUTORS-LIST:END -->
88 changes: 88 additions & 0 deletions components/ResolutionSelect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import React from 'react'
import { COLORS } from '../lib/constants'
import Checkmark from './svg/Checkmark'
import { RESOLUTIONS } from '../lib/constants'

export default class extends React.Component {
constructor(props) {
super()
this.state = { isVisible: false }
this.select = this.select.bind(this)
this.toggle = this.toggle.bind(this)
}

select(resolution) {
if (this.props.selected !== resolution) {
this.props.onChange(resolution)
}
}

toggle() {
this.setState({ isVisible: !this.state.isVisible })
}

renderResolutions() {
return RESOLUTIONS.map((resolution, i) => {
return (
<div className="list-item" key={i} onClick={this.select.bind(null, resolution.id)}>
<span style={{ Resolution: resolution.id }}>{resolution.name}</span>
{this.props.selected === resolution.id ? <Checkmark /> : null}
<style jsx>{`
.list-item {
display: flex;
align-items: center;
justify-content: space-between;
cursor: pointer;
user-select: none;
padding: 8px 16px;
border-bottom: 1px solid ${COLORS.SECONDARY};
background: rgba(255, 255, 255, 0.165);
}
.list-item:first-of-type {
border-top: 1px solid ${COLORS.SECONDARY};
}
.list-item:last-of-type {
border-bottom: none;
}
`}</style>
</div>
)
})
}

render() {
const selectedResolution =
RESOLUTIONS.filter(resolution => resolution.id === this.props.selected)[0] || {}
return (
<div className="resolution-select-container">
<div
onClick={this.toggle}
className={`display ${this.state.isVisible ? 'is-visible' : ''}`}
>
<span className="label">Resolution</span>
<span style={{ resolution: selectedResolution.id }}>{selectedResolution.name}</span>
</div>
<div className="list">{this.renderResolutions()}</div>
<style jsx>{`
.display {
display: flex;
align-items: center;
justify-content: space-between;
cursor: pointer;
user-select: none;
padding: 8px;
}
.list {
display: none;
margin-top: -1px;
max-height: 80px;
overflow-y: scroll;
}
.is-visible + .list {
display: block;
}
`}</style>
</div>
)
}
}
5 changes: 5 additions & 0 deletions components/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import enhanceWithClickOutside from 'react-click-outside'
import SettingsIcon from './svg/Settings'
import ThemeSelect from './ThemeSelect'
import FontSelect from './FontSelect'
import ResolutionSelect from './ResolutionSelect'
import Slider from './Slider'
import Toggle from './Toggle'
import WindowPointer from './WindowPointer'
Expand Down Expand Up @@ -99,6 +100,10 @@ class Settings extends React.Component {
enabled={this.props.enabled.squaredImage}
onChange={this.props.onChange.bind(null, 'squaredImage')}
/>
<ResolutionSelect
selected={this.props.enabled.resolution || '2x'}
onChange={this.props.onChange.bind(null, 'resolution')}
/>
<Toggle
label="Reset settings"
enabled={false}
Expand Down
12 changes: 11 additions & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,22 @@ export const LANGUAGES = [
}
]

export const RESOLUTIONS = [
{ id: '4x', name: '4x', value: '4' },
{ id: '2x', name: '2x', value: '2' },
{ id: '1x', name: '1x', value: '1' }
]

export const RESOLUTIONS_HASH = toHash(RESOLUTIONS)

export const LANGUAGE_MIME_HASH = toHash(LANGUAGES, 'mime')
export const LANGUAGE_MODE_HASH = toHash(LANGUAGES, 'mode')
export const LANGUAGE_NAME_HASH = toHash(LANGUAGES, 'short')

export const DEFAULT_LANGUAGE = 'auto'
export const DEFAULT_THEME = THEMES_HASH.seti
export const DEFAULT_BG_COLOR = 'rgba(171, 184, 195, 1)'
export const DEFAULT_RESOLUTION = RESOLUTIONS_HASH['2x']

export const COLORS = {
BLACK: '#121212',
Expand Down Expand Up @@ -448,5 +457,6 @@ export const DEFAULT_SETTINGS = {
fontSize: '14px',
windowControls: true,
widthAdjustment: true,
lineNumbers: false
lineNumbers: false,
resolution: '2x'
}
3 changes: 2 additions & 1 deletion lib/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const mappings = [
{ field: 'fm:fontFamily' },
{ field: 'fs:fontSize' },
{ field: 'si:squaredImage', type: 'bool' },
{ field: 'code:code' }
{ field: 'code:code' },
{ field: 'rs:resolution' }
]

const reverseMappings = mappings.map(mapping =>
Expand Down
14 changes: 10 additions & 4 deletions pages/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import {
LANGUAGE_NAME_HASH,
DEFAULT_LANGUAGE,
DEFAULT_THEME,
DEFAULT_RESOLUTION,
COLORS,
RESOLUTIONS,
RESOLUTIONS_HASH,
DEFAULT_CODE,
DEFAULT_BG_COLOR,
DEFAULT_SETTINGS
Expand Down Expand Up @@ -99,15 +102,18 @@ class Editor extends React.Component {
getCarbonImage() {
const node = document.getElementById('export-container')

const resolutionRatio = (RESOLUTIONS_HASH[this.state.resolution] || DEFAULT_RESOLUTION).value
const config = {
style: {
transform: 'scale(2)',
transform: `scale(${resolutionRatio})`,
'transform-origin': 'center',
'background': (this.state.squaredImage) ? this.state.backgroundColor : 'none'
background: this.state.squaredImage ? this.state.backgroundColor : 'none'
},
filter: n => (n.className ? String(n.className).indexOf('eliminateOnRender') < 0 : true),
width: node.offsetWidth * 2,
height: (this.state.squaredImage) ? node.offsetWidth * 2 : node.offsetHeight * 2
width: node.offsetWidth * resolutionRatio,
height: this.state.squaredImage
? node.offsetWidth * resolutionRatio
: node.offsetHeight * resolutionRatio
}

return domtoimage.toPng(node, config)
Expand Down

0 comments on commit b7adab9

Please sign in to comment.