小编soh*_*ang的帖子

反向JSON.stringify?

我正在像一个对象 {'foo': 'bar'}

如何将字符串转回对象?

javascript json object

321
推荐指数
6
解决办法
22万
查看次数

找到最大元素的位置

是否有一个标准函数返回值数组的max元素的位置(而不是值)?

例如:

假设我有一个这样的数组:

sampleArray = [1, 5, 2, 9, 4, 6, 3]
Run Code Online (Sandbox Code Playgroud)

我想要一个返回3的整数的函数,它告诉我这sampleArray[3]是数组中最大的值.

c++ algorithm

68
推荐指数
3
解决办法
8万
查看次数

为什么我不能在Python中加入这个元组?

e = ('ham', 5, 1, 'bird')
logfile.write(','.join(e))
Run Code Online (Sandbox Code Playgroud)

我必须加入它,以便我可以将其写入文本文件.

python tuples

54
推荐指数
2
解决办法
6万
查看次数

iterm vim colorscheme无法正常工作

当我从iTerm的命令行运行vim时,语法高亮似乎不能在本地工作.

例如,在vim中,我已经安装了一个很好的colorscheme,在MacVim中运行得很好但是如果在iTerm中显示相同的那个,它会很棒.

我有什么想法可以打开这个?

这是我试图使用的配色方案 http://www.vim.org/scripts/script.php?script_id=2340

macos vim iterm

31
推荐指数
4
解决办法
4万
查看次数

Three.js立方体与每张脸上的不同纹理

我正在尝试在每个面上创建一个具有不同纹理的three.js立方体.

基本上是一个骰子.这是在我的沙盒环境中,所以应该只在每侧产生一个带有骰子图像(1-6)的旋转立方体.完成后,我打算将其用于浏览器基础游戏.这个例子我只在Chrome中测试过,虽然考虑将其更改为画布渲染器以获得额外的浏览器支持.

在这里看了几个关于SO和其他大量谷歌搜索的问题,尽管我得到了答案(实际上看起来相当简单),但我根本无法让它发挥作用.

我是three.js的新手,但不是JavaScript.

我用来参考的页面是

SO - 在每张脸上都有不同纹理的three.js立方体

SO - 具有不同纹理面的three.js立方体

evanz - 测试three.js立方体

enriquemorenotent.com - three.js在每个面上构建一个具有不同材质的立方体

我的守则

var camera, scene, renderer, dice;

init();
animate();

function init() {
    renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    scene = new THREE.Scene();

    camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000);
    camera.position.set(110, 110, 250);
    camera.lookAt(scene.position);
    scene.add(camera);


    var materials = [
       new THREE.MeshLambertMaterial({
           map: THREE.ImageUtils.loadTexture('/Content/Images/dice-1-hi.png')
       }),
       new THREE.MeshLambertMaterial({
           map: THREE.ImageUtils.loadTexture('/Content/Images/dice-2-hi.png')
       }),
       new THREE.MeshLambertMaterial({
           map: THREE.ImageUtils.loadTexture('/Content/Images/dice-3-hi.png')
       }),
       new THREE.MeshLambertMaterial({
           map: THREE.ImageUtils.loadTexture('/Content/Images/dice-4-hi.png')
       }),
       new THREE.MeshLambertMaterial({ …
Run Code Online (Sandbox Code Playgroud)

javascript three.js

29
推荐指数
1
解决办法
3万
查看次数

如何使用three.js r71合并两个几何或网格?

在这里我遇到了问题,因为我需要将两个几何(或网格)合并为一个.使用早期版本的three.js有一个很好的功能:

THREE.GeometryUtils.merge(pendulum, ball);
Run Code Online (Sandbox Code Playgroud)

但是,它不再是新版本了.

我尝试合并pendulumball使用以下代码:

ball 是一个网格.

var ballGeo = new THREE.SphereGeometry(24,35,35);
var ballMat = new THREE.MeshPhongMaterial({color: 0xF7FE2E}); 
var ball = new THREE.Mesh(ballGeo, ballMat); 
ball.position.set(0,0,0);

var pendulum = new THREE.CylinderGeometry(1, 1, 20, 16);
ball.updateMatrix();
pendulum.merge(ball.geometry, ball.matrix);
scene.add(pendulum);
Run Code Online (Sandbox Code Playgroud)

毕竟,我收到以下错误:

THREE.Object3D.add: object not an instance of THREE.Object3D. THREE.CylinderGeometry {uuid: "688B0EB1-70F7-4C51-86DB-5B1B90A8A24C", name: "", type: "CylinderGeometry", vertices: Array[1332], colors: Array[0]…}THREE.error @ three_r71.js:35THREE.Object3D.add @ three_r71.js:7770(anonymous function) @ pendulum.js:20
Run Code Online (Sandbox Code Playgroud)

javascript merge three.js

27
推荐指数
4
解决办法
3万
查看次数

密码验证至少为6个字符

我为密码写了一些验证码,并确认密码是否匹配.此外,还有一个条件是检查我的密码长度是否小于6个字符,如果小于6个字符,则写入/显示错误.但是我的代码无法正常工作:当我切换到字段2时,不检查字段1的条件,如果两个条件都正确,则错误仍然存​​在.

这是我的代码:

function checkPass()
{
    var pass1 = document.getElementById('pass1');
    var pass2 = document.getElementById('pass2');
    var message = document.getElementById('error-nwl');
    var goodColor = "#66cc66";
    var badColor = "#ff6666";

    if(pass1.value == pass2.value){
        pass2.style.backgroundColor = goodColor;
        message.style.color = goodColor;
        message.innerHTML = "ok!"
    }
    else{
        pass2.style.backgroundColor = badColor;
        message.style.color = badColor;
        message.innerHTML = " These passwords don't match"
    }
	
    if(pass1.length > 5){
        pass1.style.backgroundColor = goodColor;
        message.style.color = goodColor;
        message.innerHTML = "character number ok!"
    }
    else{
        pass1.style.backgroundColor = badColor;
        message.style.color = badColor;
        message.innerHTML = " you have …
Run Code Online (Sandbox Code Playgroud)

javascript css validation jquery

24
推荐指数
8
解决办法
2万
查看次数

Python按钮功能奇怪,不做同样的事情

我目前有两个连接到我的Raspberry Pi的按钮(这些是带有环形LED的那些)并且我正在尝试执行此代码

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17, GPIO.OUT) #green LED
GPIO.setup(18, GPIO.OUT) #red LED
GPIO.setup(4, GPIO.IN, GPIO.PUD_UP) #green button
GPIO.setup(27, GPIO.IN, GPIO.PUD_UP) #red button

def remove_events():
        GPIO.remove_event_detect(4)
        GPIO.remove_event_detect(27)

def add_events():
        GPIO.add_event_detect(4, GPIO.FALLING, callback=green, bouncetime=800)
        GPIO.add_event_detect(27, GPIO.FALLING, callback=red, bouncetime=800)

def red(pin):
        remove_events()
        GPIO.output(17, GPIO.LOW)
        print "red pushed"
        time.sleep(2)
        GPIO.output(17, GPIO.HIGH)
        add_events()

def green(pin):
        remove_events()
        GPIO.output(18, GPIO.LOW)
        print "green pushed"
        time.sleep(2)
        GPIO.output(18, GPIO.HIGH)
        add_events()

def main():
    while True:
        print "waiting"
        time.sleep(0.5)

GPIO.output(17, GPIO.HIGH)
GPIO.output(18, GPIO.HIGH)
GPIO.add_event_detect(4, …
Run Code Online (Sandbox Code Playgroud)

python gpio raspberry-pi

12
推荐指数
1
解决办法
361
查看次数

Node.js ES6如何从模块中导出类?

我正在尝试从Node.js 6.2.0中的CommonJS模块导出ES6类

class MyClass{
    //class contents here
}

exports = MyClass;
Run Code Online (Sandbox Code Playgroud)

然后在另一个模块中导入它:

var MyClass = require('/path/to/module.js')
var instance = new MyClass();
Run Code Online (Sandbox Code Playgroud)

但是我得到以下异常:

TypeError: MyClass is not a constructor
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

请注意,我没有使用Babel/Tranceur,它是纯粹的JS,在最新的Node 6.2.0中实现,根据Kangax,93%实现了ES6.

//编辑:这对export和module.exports来说不是问题.虽然单独使用导出,但我得到了一些__proto__set的对象.

javascript commonjs node.js ecmascript-6

11
推荐指数
1
解决办法
2万
查看次数

Three.js动态改变光强度

有没有办法让我没有看到动态改变定向灯的光强度?甚至环境光?

ambientLight = new THREE.AmbientLight(0xffffff);
scene.add(ambientLight);            

directionalLightL = new THREE.DirectionalLight(0xffffff, dLight, 0);
directionalLightL.position.set(dlpX, dlpY, dlpZ);
scene.add(directionalLightL);
Run Code Online (Sandbox Code Playgroud)

这样做最初是为了渲染,但我怎样才能改变一个特定的光强度呢?删除/重新添加灯?在dom中找到并更改它?我没有注意到API中的东西?

javascript three.js

8
推荐指数
1
解决办法
1万
查看次数