0
캔버스를 사용하는 중에 당분간 사람의 컴퓨터에서이 캔버스로 이미지를 업로드하고 싶습니다. 나는 또한 내가 지금 설정 한 다른 2 개의 이미지와 같이 움직이고 크기를 조정할 수 있기를 원합니다. 누군가 내가 현재 가지고있는 코드를 만들 수 있습니까? 이것은 내가 지금까지 가지고있는 모든 것입니다. 그래서 여러분이 저의 일부분이 가지고있는 올바른 클래스/ids로 그런 이름을 붙일 수 있다면. 고맙습니다.KineticJs 캔버스 (이미지 업로드 포함)
<style>
canvas:active{
cursor:pointer;
}
.bg1{
background-image:url('http://s23.postimg.org/bxtmsd2tn/boutiquebase.jpg');
}
.bg2{
background-image:url('http://s4.postimg.org/bnevxi1y5/wall8.png');
}
.bg3{
background-image:url('http://s13.postimg.org/6cgbaoblh/wall9.png');
}
</style>
<div id="container" class="bg1"></div>
<img src="http://s23.postimg.org/bxtmsd2tn/boutiquebase.jpg" width="50px" id="wall1">
<img src="http://s4.postimg.org/bnevxi1y5/wall8.png" width="50px" id="wall2">
<img src="http://s13.postimg.org/6cgbaoblh/wall9.png" width="50px" id="wall3">
<table><tr>
<td>
<center> <img src="http://s29.postimg.org/yn6t21ah3/sidetable.png" id="shower" width="100px" style="cursor:pointer;">
<br>
<span id="hider1" class="sendingBut" style="cursor:pointer;">Remove </span>
</center></td>
</tr>
</table>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v5.0.2.min.js"></script>
<script>
$('#wall2').click(function() {
$('#container').removeClass('bg3').removeClass('bg1').addClass('bg2');
});
$('#wall1').click(function() {
$('#container').removeClass('bg3').removeClass('bg2').addClass('bg1');
});
$('#wall3').click(function() {
$('#container').removeClass('bg1').removeClass('bg2').addClass('bg3');
});
</script>
<script>
function update(activeAnchor) {
var group = activeAnchor.getParent();
var topLeft = group.find('.topLeft')[0];
var topRight = group.find('.topRight')[0];
var bottomRight = group.find('.bottomRight')[0];
var bottomLeft = group.find('.bottomLeft')[0];
var image = group.find('.image')[0];
var anchorX = activeAnchor.x();
var anchorY = activeAnchor.y();
// update anchor positions
switch (activeAnchor.name()) {
case 'topLeft':
topRight.y(anchorY);
bottomLeft.x(anchorX);
break;
case 'topRight':
topLeft.y(anchorY);
bottomRight.x(anchorX);
break;
case 'bottomRight':
bottomLeft.y(anchorY);
topRight.x(anchorX);
break;
case 'bottomLeft':
bottomRight.y(anchorY);
topLeft.x(anchorX);
break;
}
image.setPosition(topLeft.getPosition());
var width = topRight.x() - topLeft.x();
var height = bottomLeft.y() - topLeft.y();
if(width && height) {
image.setSize({width:width, height: height});
}
}
function addAnchor(group, x, y, name) {
var stage = group.getStage();
var layer = group.getLayer();
var anchor = new Kinetic.Circle({
x: x,
y: y,
stroke: '#666',
fill: '#ddd',
strokeWidth: 2,
radius: 8,
name: name,
draggable: true,
dragOnTop: false,
opacity: .01
});
anchor.on('mouseout', function() {
this.opacity(.01);
layer.draw()
});
anchor.on('mouseenter', function() {
this.opacity(1.00);
layer.draw()
});
anchor.on('dragmove', function() {
update(this);
layer.draw();
});
anchor.on('mousedown touchstart', function() {
group.setDraggable(false);
this.moveToTop();
});
anchor.on('dragend', function() {
group.setDraggable(true);
layer.draw();
});
// add hover styling
anchor.on('mouseover', function() {
var layer = this.getLayer();
document.body.style.cursor = 'pointer';
this.setStrokeWidth(4);
layer.draw();
});
anchor.on('mouseout', function() {
var layer = this.getLayer();
document.body.style.cursor = 'default';
this.strokeWidth(2);
layer.draw();
});
group.add(anchor);
}
function loadImages(sources, callback) {
var images = {};
var loadedImages = 0;
var numImages = 0;
for(var src in sources) {
numImages++;
}
for(var src in sources) {
images[src] = new Image();
images[src].onload = function() {
if(++loadedImages >= numImages) {
callback(images);
}
};
images[src].src = sources[src];
}
}
function initStage(images) {
var stage = new Kinetic.Stage({
container: 'container',
width: 1000,
height: 764
});
var layer = new Kinetic.Layer();
var furniGroup = new Kinetic.Group({
x: 270,
y: 100,
draggable: true
});
var yodaGroup = new Kinetic.Group({
x: 100,
y: 110,
draggable: true
});
/*
* go ahead and add the groups
* to the layer and the layer to the
* stage so that the groups have knowledge
* of its layer and stage
*/
layer.add(furniGroup);
layer.add(yodaGroup);
stage.add(layer);
document.getElementById('shower').addEventListener('click', function() {
furniImg.show();
layer.draw();
}, false);
document.getElementById('hider1').addEventListener('click', function() {
furniImg.hide();
layer.draw();
}, false);
var furniImg = new Kinetic.Image({
x: 0,
y: 0,
width: 338,
height: 285,
image: images.furni,
name: 'image'
});
furniGroup.add(furniImg);
addAnchor(furniGroup, 0, 0, 'topLeft');
addAnchor(furniGroup, 338, 0, 'topRight');
addAnchor(furniGroup, 338, 285, 'bottomRight');
addAnchor(furniGroup, 0, 285, 'bottomLeft');
furniGroup.on('dragstart', function() {
this.moveToTop();
});
var yodaImg = new Kinetic.Image({
x: 0,
y: 0,
image: images.yoda,
width: 422,
height: 243,
name: 'image'
});
yodaGroup.add(yodaImg);
addAnchor(yodaGroup, 0, 0, 'topLeft');
addAnchor(yodaGroup, 422, 0, 'topRight');
addAnchor(yodaGroup, 422, 243, 'bottomRight');
addAnchor(yodaGroup, 0, 243, 'bottomLeft');
yodaGroup.on('dragstart', function() {
this.moveToTop();
});
stage.draw();
}
var sources = {
furni: 'http://s29.postimg.org/yn6t21ah3/sidetable.png',
yoda: 'http://s28.postimg.org/58nni8dzh/blackf10.png'
};
loadImages(sources, initStage);
</script>
앵커를 추가하거나 이미지를 제거하는 방법이 너무 많아서 고맙습니다. 당신이 코드를 모두 만들지는 않겠지 만 그 이미지의 레이어와 드래그 한 레이어를 어떻게 참조 할 수 있을까요? 앵커를 추가하는 방법과 마찬가지로 furnigroup 계층을 참조하십시오. furniGroup.add (furniImg); addAnchor (furniGroup, 0, 0, 'topLeft'); addAnchor (furniGroup, 338, 0, 'topRight'); addAnchor (furniGroup, 338, 285, 'bottomRight'); addAnchor (furniGroup, 0, 285, 'bottomLeft'); –
그리고 나서 이것을 보여 주거나 숨기려면 furniimg ...를 참조하십시오. 대신 이미지 근처에 닫기 버튼을 추가해야합니다. 그러나이 코드와 기본적으로 비슷합니다. document.getElementById ('shower'). addEventListener ('클릭', function() { furniImg.show(); layer.draw(); }, false); . document.getElementById를 ('hider1') 또는 addEventListener ({) layer.draw (; furniImg.hide()를 } 함수() '클릭'거짓); –
앵커 및 제거 버튼을 추가하는 것은 질문의 범위를 벗어납니다. 그렇더라도 그룹 + 이미지 + 앵커를 코딩하는 방법에 대한 모형을 추가했습니다. 완전히 테스트되지 않았습니다. id에 의해 그룹을 가져올 수 있도록 그룹에'id' 속성을 추가했습니다. 훨씬 유연합니다 :'stage : find ("# yodoGroup")'. IMHO,이 크기 조정 방법은 다음 두 가지 방법으로 표준 이하입니다. (1) 이미지의 크기를 비례하여 조정해야합니다. 너비 또는 높이로 왜곡되지 않도록해야합니다. (2) 모든 이미지에 앵커를 추가하는 것은 리소스 돼지입니다. 대신 리팩토링하여 1 모든 이미지에 첨부 할 수있는 앵커 세트. 프로젝트에 행운을 빌어 요! – markE