에서 구성 요소를 호출하고 난 웹팩 나 등 같은 내 애플 리케이션에 어떤 번들을 사용하고 있지 않다 ... 또한만드는 방법 reactjs 내가 외부 파일에서이 구성 요소를 실행하는 반응 응용 프로그램을 만들고 싶어 외부 파일
반작용 /app.js
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import BounceCircle from './canvas';
class App extends Component {
render() {
return (
<div className="App">
<Header/>
<Content/>
<Footer/>
<BounceCircle/>
</div>
);
}
}
class Header extends Component {
render() {
return (
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1>Hello World</h1>
<h4>hello to egypt</h4>
<h3>Welcome to React</h3>
<h4>hi</h4>
</div>
);
}
}
class Content extends Component {
render() {
return (
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
);
}
}
class Footer extends Component {
render() {
return (
<h3>this the Footer</h3>
);
}
}
export default App;
그리고 여기 내가 그것을 수입하고있는 작업 할 응용 프로그램을 반응 구성 요소 코드는, 기본적으로 내가 그것을에서 실행 만들려고 HTML5의 캔버스는 응용 프로그램
캔버스 반응한다. js
외부 파일은에서import BounceCircle from './canvas'
을 그리고 당신은 다른 구성 요소처럼 사용할 수 있습니다 <BounceCircle />
가져 오기 :
class BounceCircle extends Component {
render() {
return (
//this var = canvas is to call the canvas tag from html file using querySelector
canvas=document.querySelector('canvas');
//this makes the width of the canvas the full width the screen
canvas.width = window.innerWidth;
//this makes the height of the canvas the full height the screen
canvas.height=window.innerHeight;
// this var = c is to call var canvas to start to work on it
var c=canvas.getContext("2d");
var mouse = {
x:undefined,
y:undefined
}
var maxRadius = 40;
var colorArray=[
'#FF595E',
'#33032F',
'#313E50',
'#0E7C7B',
'#87BCDE',
];
window.addEventListener('mousemove',
function(event){
mouse.x=event.x;
mouse.y=event.y;
})
window.addEventListener('resize',function()
{
//this makes the width of the canvas the full width the screen
canvas.width = window.innerWidth;
//this makes the height of the canvas the full height the screen
canvas.height=window.innerHeight;
init();
})
function Circle(x,y,dx,dy,radius){
this.x = x;
this.y = y;
this.dx= dx;
this.dy= dy;
this.radius=radius;
this.minRadius=radius;
this.color=colorArray[Math.floor(Math.random()*colorArray.length)];
this.draw = function()
{
c.beginPath();
c.arc(this.x,this.y,this.radius,0,Math.PI * 2,false);
c.fillStyle=this.color;
c.strokeStyle=this.color;
c.fill();
}
this.update = function(){
if (this.x + this.radius> innerWidth || this.x-this.radius < 0) {
this.dx = -this.dx ;
}
if (this.y + this.radius> innerHeight || this.y-this.radius < 0) {
this.dy = -this.dy ;
}
this.x += this.dx;
this.y += this.dy;
//interactivity
if (mouse.x - this.x <50 && mouse.x-this.x>-50 && mouse.y - this.y <50 && mouse.y-this.y>-50) {
if (this.radius<maxRadius) {
this.radius+= 1;
}
} else if (this.radius>this.minRadius) {
this.radius-= 1;
}
this.draw();
}
}
var circleArray=[];
for (var i = 0; i < 500; i++)
{
var radius=Math.random() * 3 + 1;
var x =Math.random()*innerWidth;
var y = Math.random()*innerHeight;
var dx= (Math.random() - 0.5)*16;
var dy= (Math.random() - 0.5)*16;
circleArray.push(new Circle(x,y,dx,dy,radius));
}
function init()
{
circleArray=[];
}
function animate(){
requestAnimationFrame(animate);
c.clearRect(0,0,innerWidth,innerHeight);
for (var i = 0; i < circleArray.length; i++)
{
circleArray[i].update();
}
}
animate();
);
}
}
export default BounceCircle;
양쪽 파일은 같은 폴더에 app.js
당신이 말할 수에서
'수입 앱? 당신이 시도한 것과 작동하지 않는 것에 대해 더 정확하게 말하십시오. –
@Damien Leroux 3, 아니오 저는 반대로하고 싶습니다. 캔버스에서 가져 오기를 원합니다 .js를 app.js로 바꿉니다. –