mirror of
https://github.com/muerwre/orchidmap-front.git
synced 2025-04-25 19:16:41 +07:00
render: poly drawing
This commit is contained in:
parent
6ed0361acf
commit
fb087c67ac
7 changed files with 76 additions and 13 deletions
|
@ -1,5 +1,6 @@
|
|||
import React from 'react';
|
||||
import { getTilePlacement } from '$utils/renderer';
|
||||
import { getPolyPlacement, getTilePlacement } from '$utils/renderer';
|
||||
import { COLORS, CONFIG } from '$config';
|
||||
|
||||
export class Renderer extends React.Component {
|
||||
componentDidMount() {
|
||||
|
@ -9,15 +10,15 @@ export class Renderer extends React.Component {
|
|||
init() {
|
||||
const ctx = this.canvas.getContext('2d');
|
||||
const geometry = getTilePlacement();
|
||||
const points = getPolyPlacement();
|
||||
|
||||
this.fetchImages(ctx, geometry)
|
||||
.then(images => this.composeImages({ geometry, images, ctx }));
|
||||
.then(images => this.composeImages({ geometry, images, ctx }))
|
||||
.then(() => this.composePoly({ geometry, points, ctx }))
|
||||
}
|
||||
|
||||
fetchImages = (ctx, geometry) => {
|
||||
const {
|
||||
minX, maxX, minY, maxY, zoom
|
||||
} = geometry;
|
||||
const { minX, maxX, minY, maxY, zoom } = geometry;
|
||||
|
||||
const images = [];
|
||||
for (let x = minX; x <= maxX; x += 1) {
|
||||
|
@ -43,18 +44,50 @@ export class Renderer extends React.Component {
|
|||
|
||||
composeImages = ({ images, geometry, ctx }) => {
|
||||
const {
|
||||
minX, minY, maxY, shiftX, shiftY, size
|
||||
minX, minY, shiftX, shiftY, size
|
||||
} = geometry;
|
||||
|
||||
images.map(({ x, y, image }) => {
|
||||
const verticalShift = (maxY - minY) * size;
|
||||
|
||||
const posX = ((x - minX) * size) + shiftX;
|
||||
const posY = ((y - minY) * size) - (verticalShift - shiftY);
|
||||
const posY = ((y - minY) * size) - shiftY;
|
||||
|
||||
return ctx.drawImage(image, posX, posY, 256, 256);
|
||||
});
|
||||
|
||||
return images;
|
||||
};
|
||||
|
||||
composePoly = ({ points, geometry, ctx }) => {
|
||||
let minX = points[0].x;
|
||||
let maxX = points[0].x;
|
||||
let minY = points[0].y;
|
||||
let maxY = points[0].y;
|
||||
|
||||
ctx.strokeStyle = 'red';
|
||||
ctx.strokeLinecap = 'round';
|
||||
ctx.strokeLinejoin = 'round';
|
||||
ctx.lineWidth = CONFIG.STROKE_WIDTH;
|
||||
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(points[0].x, points[0].y);
|
||||
|
||||
for (let i = 1; i < points.length; i += 1) {
|
||||
ctx.lineTo(points[i].x, points[i].y);
|
||||
|
||||
// gradient bounds
|
||||
if (points[i].x < minX) minX = points[i].x;
|
||||
if (points[i].x > maxX) maxX = points[i].x;
|
||||
if (points[i].y < minY) minY = points[i].y;
|
||||
if (points[i].y > maxY) maxY = points[i].y;
|
||||
}
|
||||
|
||||
const gradient = ctx.createLinearGradient(minX, minY, minX, maxY);
|
||||
gradient.addColorStop(0, COLORS.PATH_COLOR[0]);
|
||||
gradient.addColorStop(1, COLORS.PATH_COLOR[1]);
|
||||
|
||||
ctx.strokeStyle = gradient;
|
||||
ctx.stroke();
|
||||
ctx.closePath();
|
||||
};
|
||||
|
||||
render() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue