Skip to content

Instantly share code, notes, and snippets.

View lavrton's full-sized avatar

Anton Lavrenov lavrton

View GitHub Profile
const canvas = document.createElement('canvas');
const tempCanvas = document.createElement('canvas');
// make all pixells opaque 100% (except pixels that 100% transparent)
function removeTransparency(canvas) {
const ctx = canvas.getContext('2d');
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const nPixels = imageData.data.length;
for (var i = 3; i < nPixels; i += 4) {
circles.forEach(circle => {
// draw on "scene" canvas first
ctx.beginPath();
ctx.arc(circle.x, circle.y, circle.radius, 0, 2 * Math.PI, false);
ctx.fillStyle = circle.color;
ctx.fill();
// then draw on offscren "hit" canvas
hitCtx.beginPath();
hitCtx.arc(circle.x, circle.y, circle.radius, 0, 2 * Math.PI, false);
// colorsHash for saving references of all created circles
const colorsHash = {};
function getRandomColor() {
const r = Math.round(Math.random() * 255);
const g = Math.round(Math.random() * 255);
const b = Math.round(Math.random() * 255);
return `rgb(${r},${g},${b})`;
}
function hasSameColor(color, circle) {
return circle.color === color;
}
canvas.addEventListener('click', (e) => {
const mousePos = {
x: e.clientX - canvas.offsetTop,
y: e.clientY - canvas.offsetLeft
};
// get pixel under cursor
function isIntersect(point, circle) {
return Math.sqrt((point.x-circle.x) ** 2 + (point.y - circle.y) ** 2) < circle.radius;
}
canvas.addEventListener('click', (e) => {
const pos = {
x: e.clientX,
y: e.clientY
};
circles.forEach(circle => {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// create circles to draw
const circles = [
{
x: 40,
y: 40,
radius: 10,
color: 'rgb(255,0,0)'
const TargetView = connect(
(state, ownProps) => ({target: state.targets[ownProps.targetId]})
)(({ target }) => {
// your render logic
return <Circle radius={target.radius} />;
});
const TargetsList = connect(state => ({targetsOrder: state.targetsOrder}))(
({ targetsOrder }) =>
<ul>
{ targetsOrder.map((id) => <TargetView key={id} targetId={id} />) }
</ul>
);
const state = {
targetsOrder: ['id-1', 'id-2'],
targets: {
'id-1': { id: 'id-1', radius: 10 },
'id-2': { id: 'id-2', radius: 20 },
}
};
function appReducer(state, action) {
if (action.type === 'UPDATE') {
return {
target: state.target.map((target) => {
if (target.id === action.id) {
return {
...target,
radius: action.radius
};
} else {